构建PHP框架之构建模版引擎实例

零下一度
发布: 2023-03-10 17:06:02
原创
1227 人浏览过

自从来到新公司就一直很忙,最近这段时间终于稍微闲了一点,赶紧接着写这个系列,感觉再不写就烂尾了。

之前我们说到,拿到{{ $name }}这样一段内容时,我们只需要将它转化成<?php echo $name ?>这样,就可以识别了,输出相应的变量值。

那就要需要正则匹配{{ $name }},然后替换掉{{}},分别替换成<?php echo?>

但是要想到一个问题,如果我在 view 里写了 php 的代码,其中含有{{ $name }},也会被替换。例子如下:

<?php$name = &#39;test&#39;;$str = "{{ $name }}";?>
登录后复制

要解决这个问题,我们需要将 PHP 的代码去掉,只留下 html 代码再做替换的处理。幸好 PHP 有一个方法 token_get_all,会将提供的内容按 PHP 标记进行分割。使用此方法解析如下内容:

$content = <<<VIEW<?php\$name = &#39;test&#39;;\$str = "{{ \$name }}";?><html>  <body>{{ \$name }}</body><html>VIEW;print_r(token_get_all($content));
登录后复制

这里$符号前加\是为了转移,在真正是现实不需要。结果如下:

Array
(
    [0] => Array
        (
            [0] => 379
            [1] => <?php
            [2] => 1
        )
    [1] => Array
        (
            [0] => 382
            [1] =>
            [2] => 2
        )
    [2] => =
    [3] => Array
        (
            [0] => 382
            [1] =>
            [2] => 2
        )
    [4] => Array
        (
            [0] => 323
            [1] => &#39;test&#39;
            [2] => 2
        )
    [5] => ;
    [6] => Array
        (
            [0] => 382
            [1] =>
            [2] => 2
        )
    [7] => =
    [8] => Array
        (
            [0] => 382
            [1] =>
            [2] => 3
        )
    [9] => "
    [10] => Array
        (
            [0] => 322
            [1] => {{
            [2] => 3
        )
    [11] => Array
        (
            [0] => 320
            [1] => $name
            [2] => 3
        )

    [12] => Array
        (
            [0] => 322
            [1] =>  }}
            [2] => 3
        )
    [13] => "
    [14] => ;
    [15] => Array
        (
            [0] => 382
            [1] =>
            [2] => 3
        )
    [16] => Array
        (
            [0] => 381
            [1] => ?>
            [2] => 4
        )
    [17] => Array
        (
            [0] => 321
            [1] =>
<html>
  <body>{{ $name }}</body>
<html>
            [2] => 5
        )

)
登录后复制

可以看到 PHP 相关的代码被解析了,我们只需要判断出是 html 代码,然后做替换就可以了。其中的321就是定义好的常量T_INLINE_HTML的值,标记解析出来的就是 html 代码。

我们定义view文件的后缀为sf,那我们就可以在controller/model/view目录下创建view.sf文件,内容如下

<?php$title = &#39;It is a title&#39;;$str = "{{ $title }}";?><html>
    <head>
        <title>{{ $title }}</title>
    <head>
    <body>
        <h2>{{ $str }}</h2>
        <p>{{ $body }}<p>
    </body>
</html>
登录后复制

然后我们来改造Controller中的render方法,代码如下

public function render($view, $params = [])
{
    $file = &#39;../views/&#39; . $view . &#39;.sf&#39;;
    $fileContent = file_get_contents($file);
    $result = &#39;&#39;;
    foreach (token_get_all($fileContent) as $token) {
        if (is_array($token)) {
            list($id, $content) = $token;
            if ($id == T_INLINE_HTML) {
                $content = preg_replace(&#39;/{{(.*)}}/&#39;, &#39;<?php echo $1 ?>&#39;, $content);
            }
            $result .= $content;
        } else {
            $result .= $token;
        }
    }
    $generatedFile = &#39;../runtime/cache/&#39; . md5($file);
    file_put_contents($generatedFile, $result);
    extract($params);
    require_once $generatedFile;
}
登录后复制

修改actionView如下

public function actionView(){$this->render(&#39;site/view&#39;, [&#39;body&#39; => &#39;Test body information&#39;]);}
登录后复制

访问 http://localhost/simple-framework/public/index.php?r=site/view ,得到如下页面
构建PHP框架之构建模版引擎实例

今天就先到这里。项目内容和博客内容也都会放到Github上,欢迎大家提建议。

code:

blog project:

以上是构建PHP框架之构建模版引擎实例的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!