vovan-ve / html-template
Context sensitive HTML template engine
Requires
- php: ^7.1
- vovan-ve/lr0-parser: ~1.6.0
Requires (Dev)
- ext-json: *
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-10-29 05:40:29 UTC
README
Simple context sensitive HTML template engine. Yes, yet another one.
Synopsis
See an examples.
Template example:
<a href={ link } title={ title && "Foo bar: ${ title }" }>
<span id=foobar class='it parses html'>
{ description }
</span>
<span>
{# comment #}
</span>
</a>
Creating data for the template above:
use VovanVE\HtmlTemplate\runtime\RuntimeHelper; $runtime = new RuntimeHelper([ 'link' => 'http://example.com?foo=bar&lorem=ipsum#hash', 'title' => 'Lorem <ipsum> "dolor" sit amet', 'description' => function () { return 'Some <text/plain> content'; }, ]);
Run a template when everything is prepared. Here foobar
is a template's name
covered by Template Provider.
echo $engine->runTemplate('foobar', $runtime);
The output for the example above (wrapped manually only here):
<a href="http://example.com?foo=bar&lorem=ipsum#hash" title="Foo bar: Lorem <ipsum> "dolor" sit amet" ><span id="foobar" class="it parses html" >Some <text/plain> content</span><span></span></a>
Template code compiles to PHP code behind the scene. It may look something like so (formatted manually only here for demonstration):
($runtime::createElement('a', [ 'href' => ($runtime->param('link')), 'title' => (!($_ta=($runtime->param('title'))) ? $_ta :(('Foo bar: '.($runtime->param('title')))) ) ], [ ($runtime::createElement('span', [ 'id' => 'foobar', 'class' => 'it parses html' ], [ ($runtime::htmlEncode(($runtime->param('description')))) ])), '<span></span>' ]))
Compiler will evaluate as much constant expressions as possible and as much as it learned to. For example, completely constant template like following:
<div>{true && 'Lorem < ipsum dolor'}</div>
will be compiled to:
'<div>Lorem < ipsum dolor</div>'
Description
TBW.
Installation
ATTENTION! While major version number is
0
still there MAY be BC break changes in minor versions0.NEXT
, but not in revision versions0.x.NEXT
.
Install through composer:
composer require vovan-ve/html-template
or add to require
section in your composer.json:
"vovan-ve/html-template": "~0.3.0"
License
This package is under MIT License