使用str_replace进行简单模板化

时间:2010-12-22 23:32:36

标签: php templates str-replace

我只是在试验。

我想出了一个简单的方法来创建自己的想法:

class Template
{
    function parse($template_file, $braces)
    {
        if(file_exists($template_file))
        {
            $template = file_get_contents($template_file);

            foreach($braces as $brace => $replacement)
            {
                $brace = trim(strtoupper($brace));
                $build = str_replace('{' . $brace . '}', $replacement, $template);
            }

            echo $build;
        }
        else
        {
            trigger_error('Template file does not exist: ' . $template_file, E_ERROR);  
        }
    }
}

这是为了工作:

$template = new Template();

$template->parse('index_body.html', array('ONE' => 'one',
                                          'TWO' => 'two',
                                          'THREE' => 'three'));

index_body.html

{ONE}
{TWO}
{THREE}

问题是,它只输出:

{ONE} {TWO} three

它总是取代最后一个大括号,怎么不是整个阵列?

6 个答案:

答案 0 :(得分:4)

$build = str_replace('{' . $brace . '}', $replacement, $template);
                                                       ^^^^^^^^^

您总是替换原始模板,从不反对更新的模板。要么继续分配$template,要么更新$build

答案 1 :(得分:1)

$template = file_get_contents($template_file);

$build = $template;

foreach($braces as $brace => $replacement)
                {
                    $brace = trim(strtoupper($brace));
                    $build = str_replace('{' . $brace . '}', $replacement, $build);
                }

答案 2 :(得分:1)

它只替换最后一个位置,因为在每种情况下,您都要替换原始$template变量中的值。它不会在每次迭代时更新变量。

答案 3 :(得分:0)

你回显$ build,每次foreach迭代都会被重新分配。

你应该写这个

 $template = str_replace('{' . $brace . '}', $replacement, $template);

答案 4 :(得分:0)

如何使用像完整的php引擎电源(类似于smarty界面): 只是为了试验:

class Template {
    private $_file;
    private $_variables = array();

    public function __construct($file = null) {
        $this->_file = $file;
    }

    public function set($key, $value) {
        $this->_variables[$key] = $value;
    }
    public function fetch($file = null) {
        if (!$file) {
            $file = $this->_file;
        }

        extract($this->_variables);
        ob_start();

        require($file);

        $content = ob_get_contents();

        ob_end_clean();

        return $content;
    }

    public function display($file = null) {
        if (!$file) {
            $file = $this->_file;
        }

        $result = $this->fetch($file);

        echo $result;
    }
}

=============

$tpl = new Template('hello.tpl');
$tpl->set('world', 'earth');
$tpl->display();

=============
Template sample:hello.tpl

Hello <?=$world;?>

答案 5 :(得分:0)

每次迭代都会覆盖$ build。这将解决问题。

class Template
{
    function parse($template_file, $braces)
    {
        if(file_exists($template_file))
        {
            $template = file_get_contents($template_file);

            foreach($braces as $brace => $replacement)
            {
                $brace = trim(strtoupper($brace));
                $temp = str_replace('{' . $brace . '}', $replacement, $template);//create a temporary array
                $build  = array_merge($build ,$temp);
            }

            echo $build;
        }
        else
        {
            trigger_error('Template file does not exist: ' . $template_file, E_ERROR);  
        }
    }
}