PHP 中的超轻型模板系统,不允许在模板内使用 php 代码或使用 eval
我正在寻找一个非常基本的 PHP 模板系统。现在我正在使用:(
/**
* Renders a single line. Looks for {{ var }}
*
* @param string $string
* @param array $parameters
*
* @return string
*/
function renderString($string, array $parameters)
{
$replacer = function ($match) use ($parameters)
{
return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
};
return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
}
从这里:PHP - 极轻模板系统)
但我只能分配和显示变量。我还需要一种使用 IF 和循环数组等条件的方法。
我找到了 Rain TPL - http://www.raintpl.com/Quick-Start/#if - 这与我正在寻找的非常接近,但有一些我不喜欢它的地方:
- 它允许编写模板的人运行 PHP 函数(在 IF 条件内) )。
- 它写入缓存和 php 文件,这是我不想要的
那么,有没有类似的东西,但更“基本”,严格,更安全?
I'm searching for a very basic PHP templating system. Right now I'm using:
/**
* Renders a single line. Looks for {{ var }}
*
* @param string $string
* @param array $parameters
*
* @return string
*/
function renderString($string, array $parameters)
{
$replacer = function ($match) use ($parameters)
{
return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
};
return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
}
(from here: PHP - Extremely light templating system)
but I can only assign and display variables. I also need a way to use conditions like IF and loop arrays.
I found Rain TPL - http://www.raintpl.com/Quick-Start/#if - which is very close to what I'm looking for, but there are a few things that I don't like it it:
- it allows the dude who is writing the template to run PHP functions (inside the IF condition).
- it writes cache and php files, which I don't want
So, is there anything out there similar to this, but even more "basic", strict, and more secure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Twig 可能适合您。
它可以执行条件操作,并且具有针对不受信任代码的沙箱模式。
它进行编译和缓存,但这似乎可以关闭。
Twig might be for you.
It can do conditions, and has a sandbox mode for untrusted code.
It does compilation and caching, but that seems to be possible to turn off.
还有一个用于 PHP 的 Mustache 端口。 PHP 端口在这里。该语法与您已经执行的操作类似,并且支持简单的 IF 和 FOREACH 类型循环。
并且,无需评估即可完成。
There's also a Mustache port for PHP. The PHP port is here. The syntax is similar to what you're already doing, and supports simple IF and FOREACH-type loops.
And, does it without eval.
看看 Twig 或 H2O。
Have a look at Twig or H2O.
根据您的要求,我猜测您希望网站用户编写一些基本的 php 脚本。您可能找不到可以做到这一点的免费模板引擎。
我认为如果您根据需要更改现有的模板引擎,这对您来说会更好。
您可以更改 Rain TPL 以禁用某些您不需要的功能。例如,您可以执行...
禁用 IF 语句中的函数使用:
一个。找到
elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){
b.将
$this->function_check( $tag );
替换为新方法,例如$this->ifcondition_function_check( $tag );
c.创建将禁用 IF 语句中所有函数的新方法。
d.现在功能已禁用。
一个。转到方法
draw()
b.找到
unset( $this->tpl );
c.在此行之前删除已编译(缓存)文件
@unlink($this->tpl['compiled_filename']);
。d.现在缓存文件只是执行 PHP 代码的临时文件。
希望这有帮助
From your requirements I am guessing you are wanting your website users to write some basic php scripts. You might not find a free template engine that does that.
I think it's better for you if you change an existing template engine to your needs.
You can change Rain TPL to disable some of its features that you don't want. For example you can do...
Disable function use in IF statements:
a. Locate
elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){
b. Replace
$this->function_check( $tag );
with a new method something like$this->ifcondition_function_check( $tag );
c. Create the new method that will disable all functions in IF statements.
d. Now functions are disabled.
a. Go to method
draw()
b. Locate
unset( $this->tpl );
c. Just before this line remove the complied (cache) file
@unlink($this->tpl['compiled_filename']);
.d. Now the cache file is just a temporary file to execute the PHP code.
Hope this helps
非常容易使用
http://www.smarty.net/
very easy to use
http://www.smarty.net/
当您想要它非常小且灵活时,也许最好的方法就是保留您自己的东西?我喜欢手工制作;-) 您可以扩展您现有的功能。接下来,您的函数加上
if
和loop
语句以及出于安全考虑的变量转义:此脚本在 PHP 5.3 中进行了测试,您可以将其 1:1 复制到文件中进行播放与它。
When you want it really small and flexible maybe the best is to stay with your own stuff? I like handcrafting ;-) You can extend your existing function. Following, your function plus
if
andloop
statement and escaping of variables for security:This script is tested in PHP 5.3 and you can copy it 1:1 to a file to play with it.
尝试 PHPTAL: http://phptal.org/
TAL 模板的语法不会破坏 html,因此您 - 并且设计师可以检查它们是否看起来不错。
另请参阅:
http://wiki.zope.org/ZPT/TALSpecification14
http://wiki.zope.org/ZPT/TAL
try PHPTAL: http://phptal.org/
the syntax for TAL templates does not break html, so you - and the designers can check if they going to look good.
see also:
http://wiki.zope.org/ZPT/TALSpecification14
http://wiki.zope.org/ZPT/TAL