Heim > Backend-Entwicklung > PHP-Tutorial > 自个儿默写smarty模板引擎,不解析php呢(求高手帮忙)

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

WBOY
Freigeben: 2016-06-13 12:11:29
Original
1227 Leute haben es durchsucht

自己默写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 />
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage