savageman/zenml

将类似Hamlbar的字符串转换为Handlebars模板

dev-master 2014-01-30 22:25 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:41:10 UTC


README

我爱Handlebars,但我发现它仍然缺少基于缩进的DOM树(如Haml)以及ID和类的Emmet/CSS语法。

因此,我制作了这个预处理器来解决这个问题。它生成Handlebars。

快速指南

  • 包含HTML标签的行以 % 开头;
  • 缩进用于创建DOM树;
  • 缩进也适用于Handlebars {{#helpers}}
  • 使用常规HTML语法添加属性:title="这是标题"
  • 文本节点可以添加为
    • 新行使用缩进
    • 作为当前标签的 text 属性:text="这是我的文本"
  • 任何无法识别的行都被解释为纯文本。

示例

%h1 text="This is my title"

%div #container

    {{ #each items }}
        %h2 text="{{title}}"
        %span .date text="{{date}}"
        %div .description
            The description is a bit longer,
            so I split it on several lines.

    {{ else }}
        %p text="Nothing here..."

%footer
    This is the end of the example.

转换为

<h1>This is my title</h1>

<div id="container">

    {{#each items}}
        <h2>{{title}}</h2>
        <span class="date">{{date}}</span>
        <div class="description">
            The description is a bit longer,
            so I split it on several lines.
        </div>

    {{else}}
        <p>Nothing here...</p>
    {{/each}}
</div>

<footer>
    This is the end of the example.
</footer>

用法

$template = <<<TEMPLATE
%div id="test"
    %p text="First line"
    %p text="Second line"
Plain-text for the end
TEMPLATE;

$zenml = new Zenml\Zenml(array(
    'prepend' => '',
    'input_indentation' => '    ',
    'output_indentation' => '    ',
));

echo htmlspecialchars($zenml->render($template));

局限性

(如果有人能帮我整理这些问题,我会很高兴。)

  • 标签名与CSS-like #id.classes 属性之间必须有空格;
  • 文本节点必须在新的行上(如果允许它在行尾会更好);
  • 每行只能有一个标签。

待办事项