Efficient way to create and modify HTML tags.
Sometimes you need to return html tag from class, and be able to modify it later:
<?php
class myImage{
public static function getHtml(){
return '<img src="myimage.png" />';
}
}
$img = myImage::getHtml();
// I wanna add alt and style attributes before output, but how I can?
echo $img;
<?php
use yuki\html\tag;
class myImage{
public static function getHtml(){
return tag::create('img', array('src' => 'myimage.png'));
}
}
$img = myImage::getHtml();
Now you can do what you want:
<?php
$img['alt'] = 'My Image!';
$img['style'] = "border: none;";
echo $img; // <img src="myimage.png" alt="My Image!" style="border: none;" />
You even able to get attributes
<?php
echo $img['src']; // myimage.png
To remove attributes
<?php
unset($img['style']);
echo $img; // <img src="myimage.png" alt="My Image!" />
To wrap
<?php
use yuki\html\tag;
$a = tag::create('a', array('href'=>'/'));
$a->appendChild($img);
echo $a; // <a href="/"><img src="myimage.png" alt="My Image!" /></a>
To set text, it will be escaped!
<?php
$a->setText('click here >>');
echo $a; // <a href="/">click here >></a>
<?php
use yuki\html\tag;
$head = tag::create('head');
$head->appendChild(
tag::create('title')->setText('Page Title')
);
$head->appendChild(
tag::create('meta', array(
'name'=>'description',
'contents' => 'Page Description'
)
);
$head['lang'] = 'en';
echo $head; // Will output generated html code.
- Any flavor of PHP 5.3 should do
- [optional] PHPUnit 3.5+ to execute the test suite (phpunit --version)
Bugs and feature request are tracked on Github
olamedia - [email protected]
See also the list of contributors which participated in this project.
yuki-html is licensed under the MIT License - see the LICENSE file for details
This library is inspired by symfony (TagHelper), kohana (HTML) and other frameworks