Location: PHPKode > projects > tgsf > tgsf-0.9.2/docs/tgsfHtmlTag.txt
#summary How to use tgsfHtmlTag

= Contents =
<wiki:toc max_depth="3" />

=Introduction=

tgsf has a highly useful html generation object that is used as the basis for many core classes:
<br><br>
TgsfGrid<br>
TgsfReport<br>
TgsfForm<br>
BreadCrumb<br>

It can be used to programmatically create a content structure along with attributes and children elements.
==Instantiating==
*Traditional Example*
{{{
<?php
$anchor = new tgsfHtmlTag( 'a' );
$anchor->addAttribute( 'href', 'http://www.example.com/' );
$anchor->content( 'Click Here' );
echo $anchor
?>
}}}
*Output*
{{{
<a href="http://www.example.com/">Click Here</a>
}}}

*Multi-Line-Chain Example*
{{{
<?php
echo tgsfHtmlTag::factory( 'a' )
    ->addAttribute( 'href', 'http://www.example.com/' )
    ->content( 'Click Here' );
?>
}}}
*Output*
{{{
<a href="http://www.example.com/">Click Here</a>
}}}


==Adding Child Tags==
Add a child tag by using the `->addTag()` method


->addTag accepts two different types of arguments.  Passing a string creates a new tag for that string.
<br>
*Example*
{{{
<?php

echo tgsfHtmlTag::factory()
}}}
If you pass a string to it
it will create a new tag using that.  If you pass an existing tgsfHmtlTag
object, it will use that instead.  Existing tag objects are cloned so that
any changes made to the tag after being added won't carry back out to
the original object.  ->addTag always returns the tag that has been
inserted into the parent tgsfHtmlTag object.

Our examples are using a vertically
oriented object method chaining syntax - don't be alarmed, this works
and (in our opinion) makes for pretty and easily read code.

Example:
$divTag = new tgsfHtmlTag( 'div' )
	->css_class( 'my-div' )
	->addTag( 'p' )
		->content( 'my paragraph content' );
		
$tag = new tgsfHtmlTag( 'body' )
	->addTag( $divTag )
		->addTag( 'p' )
			->content( 'this is the second paragraph in my one div' );

echo $tag->render();		
echo $divTag->render();

This will output (actual output contains no new lines or tabs):
Keep in mind that we clone the original $divTag so the extra p tag
that is added after we add it to our body object doesn't exist
in the original $divTag.

<body>
<div class="my-div">
	<p>my paragraph content</p>
	<p>this is the second paragraph in my one div</p>
</div>
</body>
<div class="my-div">
	<p>my paragraph content</p>
</div>

=
Content Filtering
-----------------

When creating a tag, you may define a content filter.
Example:
$tag = new tgsfHtmlTag( 'p' )
	->contentFilter( 'nl2br' );
	
To clear a filter:

$tag->contentFilter( null );
	
This is used extensively in grids. - See grid documentation.
Return current item: tgsf