Ale*_*lex 14 php parsing templates template-engine
我正在寻找一个非常基本的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);
}
Run Code Online (Sandbox Code Playgroud)
(从这里:PHP - 极轻的模板系统)
但我只能分配和显示变量.我还需要一种方法来使用IF和循环数组等条件.
我找到了Rain TPL - http://www.raintpl.com/Quick-Start/#if - 这与我正在寻找的非常接近,但有些事我不喜欢它:
那么,有没有类似的东西,但更"基本",严格,更安全?
根据您的要求,我猜测您希望网站用户编写一些基本的 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 语句中的所有函数的新方法。
private function ifcondition_function_check($code)
{
$preg = '/[a-zA-z0-9]+\((.*?)\)/';
if (preg_match( $preg, $code, $match ) ){
// find the line of the error
$line = 0;
$rows=explode("\n",$this->tpl['source']);
while( !strpos($rows[$line],$code) )
$line++;
// draw the error line
$error = str_replace( array('<','>'), array( '<','>' ), array($code,$rows[$line]) );
$error = str_replace( $code, "<font color=red>$code</font>", $rows[$line] );
// debug the error and stop the execution of the script
die( "<div>RainTPL Sandbox Error in template <b>{$this->tpl['tpl_filename']}</b> at line $line : <i>$error</i></b>" );
}
}
Run Code Online (Sandbox Code Playgroud)
d. 现在功能被禁用。
draw()
unset( $this->tpl );
@unlink($this->tpl['compiled_filename']);
。希望这可以帮助