Efficient way to create and modify HTML tags.
Sometimes you need to return html tag from class, and be able to modify it later: class myImage{ public static function getHtml(){ return ''; } } $img = myImage::getHtml(); // I wanna add alt and style attributes before output, but how I can? echo $img;
class myImage{
public static function getHtml(){
return yHtmlTag::create('img', array('src' => 'myimage.png'));
}
}
$img = myImage::getHtml();
Now you can do what you want: $img['alt'] = 'My Image!'; $img['style'] = "border: none;"; echo $img; // You even able to get attributes echo $img['src']; // myimage.png To remove attributes unset($img['style']); echo $img; // To wrap $a = yHtmlTag::create('a', array('href'=>'/')); $a->appendChild($img); echo $a; // To set text, it will be escaped! $a->setText('click here >>'); echo $a; // click here >>
MIT
$head = yHtmlTag::create('head');
$head->appendChild(
yHtmlTag::create('title')->setText('Page Title')
);
$head->appendChild(
yHtmlTag::create('meta', array(
'name'=>'description',
'contents' => 'Page Description'
)
);
$head['lang'] = 'en';
echo $head; // Will output generated html code.