I'm trying to find a tool that I can use to generate markup programatically (non-irl word). Here is an example of what I'm trying to accomplish in pseudo-code...
$htmlDoc = new HTML_Document();
$htmlDoc->setTitle('Cool Title');
$htmlDoc->addJs('some_js_file.js');
$htmlDoc->addStylesheet('some_css_file.css');
$divElement = new HTML_Element_Div();
$htmlDoc->append('body', $divElement);
$output = $htmlDoc->generate();
What is the best tool for this?
I'm working on a simple DSL that addresses this common task. It's called htmlgen, mirrored on packagist.
Here's an example -
use function htmlgen\html as h;
h('#wrapper',
h('h1.title', 'Hello, World'),
h('p',
h('comment', 'link to something'),
h('a', ['href'=>'https://duckduckgo.com'], 'search the internet')
)
);
Here's the output (actual output does not have whitespace)
<div id="wrapper">
<h1 class="title">Hello, World</h1>
<p>
<!-- link to something -->
<a href="https://duckduckgo.com">search the internet</a>
</p>
</div>
It's very new but at ~200 lines of source, it's already very powerful. Fork it and make adjustments or send me a note with suggestions.
There's one bundled as standard in PHP: http://php.net/manual/en/book.dom.php
C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With