Elementary
, (*1)
An HTML generator for PHP., (*2)
The following example code:, (*3)
html(
$el->head(
$el->meta_(array('charset'=> 'utf-8')),
$el->title('Elementary Example')
),
$el->body(
$el->h1('Elementary Demonstration & Examples'),
$el->div(
array('id' => 'contents'),
$el->p('This is an example paragraph.')
)
)
);
?>
Produces the following structure without the indentation and line-breaks., (*4)
<html>
<head>
<meta charset="utf-8" />
<title>Elementary Example</title>
</head>
<body>
<h1>Elementary Demonstration & Examples</h1>
<div id="contents">
<p>This is an example paragraph.</p>
</div>
</body>
</html>
Empty Elements
To create an empty element like <br />
, or <img />
, append the element name with an underscore '_'., (*5)
img_(array('src' => 'bartman.png'));
//
?>
Fragments
Sometimes you might want to hold elements temporarily before adding them to a node. The way to do this is by creating Fragments similar in spirit to DOM's HTML Fragments., (*6)
_($el->p('Foo.'), $el->p('Bar.'));
?>
You can then add add more elements to it if you like., (*7)
add($el->p('Baz.'));
?>
When you add the fragment to an element and print it, it will take the contents of the fragment as if they were its own. Like how:, (*8)
div($fragment);
?>
...becomes, (*9)
<div>
<p>Foo.</p>
<p>Bar.</p>
<p>Baz</p>
</div>
Caveats
- Default serialization is xml (i.e. it looks like XHTML with empty elements closed with '/')
- Does not create comments
- Right now, Elementary produces 'ugly' HTML. 'Ugly' means no line-breaks and no indentations.
- To force a leading or trailing space, you can add an empty space (' ') before or after the elements
FAQ
Q. Is it fast?, (*10)
Don't know. Haven't benchmarked it. I don't think it is but it's fast enough for me., (*11)