Home  >  Article  >  Backend Development  >  自个儿默写smarty模板引擎,不解析php呢(求高手帮忙)

自个儿默写smarty模板引擎,不解析php呢(求高手帮忙)

WBOY
WBOYOriginal
2016-06-13 12:11:29972browse

自己默写smarty模板引擎,不解析php呢(求高手帮忙)
  $realcontent  =$this->filecontent;


$mode = '/\{\$([\w]+)\}/';
if(preg_match($mode,$realcontent)){

var_dump($assign_vars);

$realcontent=preg_replace($mode,"assign_vars['$1'] ?>",$realcontent); ;//替换成index.php文件中注入的变量


}
return $realcontent;

 
 }  

运行后代码为
assign_vars['content'] ?>

作者:assign_vars['name'] ?>assign_vars['var'] ?>

------解决思路----------------------

<br /><br /><?php<br /><br />$realcontent = file_get_contents("template_test.html");<br />$tpl_string = <<< TEMPLEATE<br /><html><br />    <head><br />        <title>{\$title}</title><br />        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br />    </head><br />    <body><br />        <div>作者:{\$name}</div><br />        <div>国家:{\$china}</div><br />        <div>省份:{\$provice}</div><br />        <div>城市:{\$city}</div><br />    </body><br /></html><br />TEMPLEATE;<br />$mode = '/\{\$(\w+)\}/';<br />$assign_arr = array();<br />function assign($name, $value) {    <br />    global $assign_arr;<br />    $assign_arr[$name] = empty($value) ? '' : $value;    <br />    return $assign_arr;<br />}<br />function replace_content($match) {<br />    global $assign_arr;<br />    if (!empty($match)) {        <br />        return array_key_exists($match[1], $assign_arr) ? $assign_arr[$match[1]] : '';<br />    }<br />}<br />assign("title", "I love you");<br />assign("name", "iseagold");<br />assign("china", "中国");<br />assign("provice", "广东省");<br />assign("city", "深圳市");<br />assign("no", "页面不存在,未取出的变量");<br />echo preg_replace_callback($mode, "replace_content", $tpl_string);<br /><br /><br />//===================================简单的类版本==========================================================<br />class Template {<br />    public $assign_items = array();<br />    public $content;<br />    <br />    function __construct($file) {<br />        $this->content = file_get_contents($file);<br />    }<br />    function assign($name, $value) {<br />        $this->assign_items[$name] = empty($value) ? "" : $value;<br />    }<br />    function replace() {<br />        $arr_assign_items = array();<br />        $content = '';<br />        $content = $this->content;<br />        $arr_assign_items = $this->assign_items;        <br />        $pattern = '/\{\$(\w+)\}/';<br />        $this->content = preg_replace_callback($pattern, function($match) use($arr_assign_items) {<br />            if (!empty($match)) {                      <br />                <br />                if (array_key_exists($match[1], $arr_assign_items)) {<br />                    echo $arr_assign_items[$match[1]];<br />                    return $arr_assign_items[$match[1]];<br />                }<br />            }<br />        }, $content);<br />    }<br />    function display() {<br />        echo $this->content;<br />    }<br />}<br />$template = new Template("template_test.html");<br />$template->assign("title", "I love you");<br />$template->assign("name", "iseagold");<br />$template->assign("china", "中国");<br />$template->assign("provice", "广东省");<br />$template->assign("city", "深圳市");<br />$template->assign("no", "页面不存在,未取出的变量");<br />$template->replace();<br />$template->display();<br />?><br /><br /><br />

------解决思路----------------------
你只产生了 php 代码,并没有执行他

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn