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.
Since all template languages will compile to PHP anyway and speed is a factor why not use embedded PHP statements? It's as fast as can get and won't need any third party hocus pocus.
PHP is a templating language to start with
<div>
<?php if (true): ?>
<h1>It's true</h1>
<?php else: ?>
<h1>It's false</h1>
<?php endif; ?>
</div>
Since users can edit the templates the above won't be much of an option. The freedom to do whatever you wan't is a huge security risk. So you'll need a templating engine to restrict what's possible.
I always found Twig to be a very good templating engine. It's pretty fast without caching and easy to include in your project. Definitely worth a look.
With Twig it's also possible to sandbox your users templates with the sandbox extension.