## Class mr_html_tag

Description

MR HTML Tag

This class is designed to generate HTML tags with attributes.

The class relies on the fluent interface in order to build html tags with their attributes.

The first call is always a call to a function whose name is the name of the HTML tag you want to create.

Example:

1. <?php
2. $tag = new mr_html_tag();
3. // Generate an <a> tag
4. $html = $tag->a('Click me!')->href('http://foo.bar')->close();
5. // $html is set to <a href="http://foo.bar">Click me!</a>
7. // Generate a <div> tag
8. $html = $tag->div('Some words')->class('centerpara')->close();
9. // $html is set to <div class="centerpara">Some words</div>
10. ?>

Also, there is a static interface.

Example (this is equivalent to the above example):

1. <?php
2. $html = mr_html_tag::open()->a('Click me!')->href('http://foo.bar')->close();
3. $html = mr_html_tag::open()->div('Some words')->class('centerpara')->close();
4. ?>

There are a couple of ways to modify attributes. Review the rest of the class methods and see example code for recommended ways to call it.

- author: Mark Nielsen
- example: See this class in action

Located in /html/tag.php (line 69)

Variable Summary
object $attributes
string $contents
string $tag
Method Summary
static mr_html_tag open ()
mr_html_tag __construct ()
mr_html_tag add_attribute (string $name, mixed $value)
mr_html_tag add_attributes (mixed $attributes)
mr_html_tag append_attribute (string $name, mixed $value)
mr_html_tag append_attributes (mixed $attributes)
string close (string $param)
mixed get_attribute (string $name)
void init (string $tag, array $contents)
mr_html_tag prepend_attribute (string $name, mixed $value)
mr_html_tag prepend_attributes (mixed $attributes)
mr_html_tag remove_attribute (string $name)
mr_html_tag remove_attributes (array $attributes)
mixed __call (string $name, string $arguments)
string __toString ()
Variables
object $attributes (line 89)

HTML tag attribute list

- access: protected

string $contents = NULL (line 82)

HTML tag contents (EG: goes between the open/close tags)

- access: protected

string $tag = NULL (line 75)

HTML tag name

- access: protected

Methods
static open (line 215)

Static interface

Example

1. <?php
2. $html = mr_html_tag::open()->a('Click me!')->href('http://foo.bar')->close();
3. ?>

- access: public

static mr_html_tag open ()
Constructor __construct (line 94)

Constructor

- access: public

mr_html_tag __construct ()
add_attribute (line 290)

Get an attribute

Recommended way to call:

1. <?php
2. $tag = new mr_html_tag();
3. $link = $tag->a('Click me!'); // Create a link tag
4. $link->href('http://foo.bar'); // Calls add_attribute('href', 'http://foo.bar')
5. ?>

- access: public

mr_html_tag add_attribute (string $name, mixed $value)

- string $name: The attribute name
- mixed $value: The attribute value

add_attributes (line 301)

Bulk add attributes

- access: public

mr_html_tag add_attributes (mixed $attributes)

- mixed $attributes: Array or object of atrributes

append_attribute (line 325)

Append an attribute

Recommended way to call:

1. <?php
2. $tag = new mr_html_tag();
3. $link = $tag->a('Click me!')->href('http://foo.bar')->class('foo');
4. $link->append_class('bar'); // Calls append_attribute('class', 'bar')
5. // and class attribute will render as class="foo bar"
6. ?>

- access: public

mr_html_tag append_attribute (string $name, mixed $value)

- string $name: The attribute name
- mixed $value: The attribute value to append

append_attributes (line 340)

Bulk append attributes

- access: public

mr_html_tag append_attributes (mixed $attributes)

- mixed $attributes: Array or object of atrributes

close (line 410)

Render the HTML tag

This method will render the tag with all of its attributes and content. You can pass additional strings to this method to add more content to the tag.

Example code:

1. <?php
2. $tag = new mr_html_tag();
3. $html = $tag->div()->class('generalbox')->close(
4. $somestring1,
5. $somestring2,
6. $tag->p($somestring3)
7. );
8. ?>

- throws: coding_exception
- access: public

string close (string $param)

- string $param: Pass as many strings as you like, all will be added to the contents of the tag

get_attribute (line 234)

Get an attribute

Recommended way to call:

1. <?php
2. $tag = new mr_html_tag();
3. $link = $tag->a('Click me!')->href('http://foo.bar');
4. $href = $link->get_href(); // Calls get_attribute('href') and returns 'http://foo.bar'
5. ?>

- access: public

mixed get_attribute (string $name)

- string $name: The attribute name

init (line 109)

Init routine

This is protected because it should only be called by this class itself through mr_html_tag::__call()

- access: protected

void init (string $tag, array $contents)

- string $tag: The HTML tag name
- array $contents: The HTML tag contents

prepend_attribute (line 364)

Prepend an attribute

Recommended way to call:

1. <?php
2. $tag = new mr_html_tag();
3. $link = $tag->a('Click me!')->href('http://foo.bar')->class('foo');
4. $link->prepend_class('bar'); // Calls prepend_attribute('class', 'bar')
5. // and class attribute will render as class="bar foo"
6. ?>

- access: public

mr_html_tag prepend_attribute (string $name, mixed $value)

- string $name: The attribute name
- mixed $value: The attribute value to prepend

prepend_attributes (line 379)

Bulk prepend attributes

- access: public

mr_html_tag prepend_attributes (mixed $attributes)

- mixed $attributes: Array or object of atrributes

remove_attribute (line 256)

Remove an attribute

Recommended way to call:

1. <?php
2. $tag = new mr_html_tag();
3. $link = $tag->a('Click me!')->href('http://foo.bar');
4. $link->remove_href(); // Calls remove_attribute('href') and unsets href attribute
5. ?>

- access: public

mr_html_tag remove_attribute (string $name)

- string $name: The attribute name

remove_attributes (line 267)

Bulk remove attributes

- access: public

mr_html_tag remove_attributes (array $attributes)

- array $attributes: An array of attribute names

__call (line 163)

This is where all of the real work is done.

Special method calls include:

- append_ATTRIBUTENAME($value) (Same as mr_html_tag::prepend_attribute(ATTRIBUTENAME, $value))
- prepend_ATTRIBUTENAME($value) (Same as mr_html_tag::prepend_attribute(ATTRIBUTENAME, $value))
- remove_ATTRIBUTENAME() (Same as mr_html_tag::remove_attribute(ATTRIBUTENAME))
- remove_ATTRIBUTENAME() (Same as mr_html_tag::get_attribute(ATTRIBUTENAME))

How to use:

1. <?php
2. $tag = new mr_html_tag();
3. $link = $tag->a('Click me!'); // This will return a new instance
4. // mr_html_tag and set the tag='a'
5. // and contents='Click me!'
6. $link->href('http://foo.bar'); // This will add an attribute to the a
7. // tag: href="http://foo.bar" (Same as
8. // mr_html_tag::add_attribute()
9. $html = $link->close(); // This will close the tag and return the HTML
11. // All of the above calls can be stringed together using
12. // the fluent interface to get the HTML
13. $html = $tag->a('Click me!')
14. ->href('http://foo.bar')
15. ->close();
16. ?>

- throws: coding_exception
- access: public
- example: See more examples

mixed __call (string $name, string $arguments)

- string $name: The function name called
- string $arguments: The function arguments passed

__toString (line 122)

Convert this object to it's HTML string equivalent

- access: public

string __toString ()

Documentation generated on Thu, 28 Jun 2012 16:33:52 -0700 by phpDocumentor 1.4.3