vovan-ve/html-template

上下文相关的HTML模板引擎

v0.3.4 2018-10-17 09:25 UTC

README

Latest Stable Version Latest Dev Version Build Status License

简单的上下文相关的HTML模板引擎。是的,又一个。

摘要

请参阅 示例

模板示例

<a href={ link } title={ title && "Foo bar: ${ title }" }>
    <span id=foobar class='it parses html'>
        { description }
    </span>
    <span>
        {# comment #}
    </span>
</a>

为上述模板创建数据

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';
    },
]);

在一切准备就绪后运行模板。在这里 foobar 是由模板提供者覆盖的模板名称。

echo $engine->runTemplate('foobar', $runtime);

上述示例的输出(此处手动包装)

<a href="http://example.com?foo=bar&amp;lorem=ipsum#hash"
title="Foo bar: Lorem &lt;ipsum&gt; &quot;dolor&quot; sit amet"
><span id="foobar" class="it parses html"
>Some &lt;text/plain&gt; content</span><span></span></a>

模板代码在幕后编译为PHP代码。它可能看起来像这样(此处仅手动格式化以进行演示)

($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>'
]))

编译器将尽可能多地评估常量表达式和它所学习的内容。例如,以下完全常量模板

<div>{true && 'Lorem < ipsum dolor'}</div>

将编译为

'<div>Lorem &lt; ipsum dolor</div>'

描述

TBW.

安装

注意! 当主要版本号为 0 时,次要版本号 0.NEXT 中可能会有BC(向后兼容性)更改,但修订版本号 0.x.NEXT 中不会有。

通过 composer 安装

composer require vovan-ve/html-template

或将以下内容添加到您的 composer.json 中的 require 部分

"vovan-ve/html-template": "~0.3.0"

许可证

此软件包受 MIT 许可证 的约束。