0

For a project I'm working on, I need to parse a tiny flow-control language in PHP.

Smarty syntax would be ideal, but we wouldn't be able to use Smarty's caching and would only need a small subset of it's features (basically just {if}, {else}, {elseif}, {/if} and function plugins).

Despite the lack of caching, performance is a major factor; including the entire Smarty framework and disabling caching is too slow.

Is there any PHP framework that can handle Smarty-like syntax without any of the more advanced features. Doesn't need to be a perfect match or even a close match, just something I can mix with plain HTML.

Any code that parses and handles the if .. elseif .. else .. logic would be appreciated too; I can hack in the rest myself.

Forgot to mention earlier; the templates are editable by users, so security is a concern.

4

1 回答 1

0

既然所有模板语言无论如何都会编译成 PHP,而且速度是一个因素,为什么不使用嵌入式 PHP 语句呢?它尽可能快,并且不需要任何第三方 hocus pocus。

PHP 是一门模板语言

<div>
    <?php if (true): ?>
        <h1>It's true</h1>
    <?php else: ?>
        <h1>It's false</h1>
    <?php endif; ?>
</div>

由于用户可以编辑模板,因此上述选项不会太多。为所欲为的自由是一个巨大的安全风险。所以你需要一个模板引擎来限制什么是可能的。

我一直发现Twig是一个非常好的模板引擎。它在没有缓存的情况下非常快,并且很容易包含在您的项目中。绝对值得一看。

使用 Twig,还可以使用沙箱扩展对您的用户模板进行沙箱处理。

http://twig.sensiolabs.org/doc/api.html#sandbox-extension

于 2013-05-17T13:07:56.740 回答