首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP模板引擎

PHP模板引擎
EN

Stack Overflow用户
提问于 2010-06-30 15:09:55
回答 3查看 1.1K关注 0票数 0

如何改进下面的脚本以便能够添加全局模板部分,如页面的开始和结束?

代码语言:javascript
复制
  <?php
    class simpleTemplate {
        var $variables;
        var $variables_callback;
        var $contents;
        var $preg;
        var $regexps;
        // Constructor, initialize default variables
        function simpleTemplate(){
            // default sub regexp matches
            $this->preg['var'] = '[a-zA-Z.\-_]{1,50}';

            //INSERT REGEXP

            $this->php_start = "\necho '";
            $this->php_end = "';\n";
            // Main find-replace regexps
            $this->regexps = array(
                // echo-variable statement: {(var)}
                'echo' => array(
                    'search' => '~\{((?:'.$this->preg['var'].')(?:\.)?)\}~sU',
                    'replace' => "'.\$this->_get('\\1').'"
                )
       'include' => array(
           'search' => '', // UPDATE
                    'replace' => "" // UPDATE
       )
            );
        }
        // Load a file and convert it into native PHP code
        function load($file){
            if (!is_readable($file))
                return FALSE;
            $this->file = $file;
            $this->contents = NULL;
            if (!isset($this->contents)){
                $this->contents = file_get_contents($this->file);
                $this->parse();
            }
        }
        // Load a converted template, apply variables and echo the output
        function show(){
            ob_start();
            eval($this->contents);
            ob_end_flush();
        }
        // Main converter, call sub-convertors and perform some cleaning
        function parse(){
            $this->contents = str_replace("'", "\'", $this->contents);
            $this->contents = $this->php_start.$this->contents.$this->php_end;
            foreach ($this->regexps as $regexp)
                do {
                    if (isset($contents))
                        $this->contents = $contents;
                    $contents = preg_replace($regexp['search'], $regexp['replace'], $this->contents);
                } while ($contents != $this->contents);
            $this->clean();
        }
        // Clean trash in generated PHP code
        function clean(){
            $this->contents = preg_replace("~([^\\\])''\.~", '\1', $this->contents);
            $this->contents = str_replace(".''", "", $this->contents);
            $this->contents = str_replace("\necho '';\n", "", $this->contents);
            $this->contents = str_replace("else {}", "", $this->contents);
            // Remove all whitespace from the template output
            //$this->contents = preg_replace("~(\s{2,}|\n)~", '', $this->contents);
            return;
        }
        // Get a variable from internal, or an external source
        function _get($variableName){
            $variable = @eval('return $this->variables[\''.str_replace('.', "']['", $variableName).'\'];');
            if (!isset($variable)) {
                if (isset($this->variables[$variableName]))
                    $variable = $this->variables[$variableName];
                else {
                    // Support for an external variable-source
                    $function_name = $this->variables_function;
                    if (isset($variables_callback))
                        $variable = call_user_func($variables_callback, $variableName);
                }
            }
            if (!isset($variable))
                return FALSE;
            else
                return $variable;
        }
        // Set an internal variable
        function _set($variableName, $value){
            $this->variables[$variableName] = $value;
            return TRUE;
        }
    }
    ?>

示例.php页面如下所示

代码语言:javascript
复制
<?php
require_once('tpl.php');     
$tpl = new simpleTemplate();
$data['user'] = array(
        'name' => 'Denis',
        'surname' => 'Bobrovnikov'
);
$tpl->variables = $data;
$tpl->load('index.tpl');
$tpl->show();
?>

模板如下

代码语言:javascript
复制
My name is {user.name} {user.surname}

我希望能够在模板文件中这样做。

代码语言:javascript
复制
{include=header.tpl}
My name is {user.name} {user.surname}
{include=footer.tpl}

其中页眉和页脚也被解析。

EN

回答 3

Stack Overflow用户

发布于 2010-06-30 15:23:36

使用现有的模板引擎,如果您想要在PHP之上的东西,不要重新发明车轮。

给小枝一根http://www.twig-project.org/

票数 2
EN

Stack Overflow用户

发布于 2010-06-30 15:21:17

1)采用MVC模式。

2) PHP是模板引擎。为什么不用它呢?

票数 1
EN

Stack Overflow用户

发布于 2010-06-30 15:32:22

作为一个程序员,我的职责是下载我自己的号角,希望我能有所帮助。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3150590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档