0

とりあえず実験中です。

ここで簡単な方法で自分で作成するというアイデアを思いつきました:

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

常に最後の中括弧を置き換えますが、配列全体ではないのはなぜですか?

4

6 に答える 6

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

更新されたテンプレートに対してではなく、常に元のテンプレートに対して置き換えます。を割り当て続けるか$template、更新します$build

于 2010-12-22T23:35:48.747 に答える
1
$template = file_get_contents($template_file);

$build = $template;

foreach($braces as $brace => $replacement)
                {
                    $brace = trim(strtoupper($brace));
                    $build = str_replace('{' . $brace . '}', $replacement, $build);
                }
于 2010-12-22T23:35:50.887 に答える
1

いずれの場合も、元の$template変数の値を置き換えているため、最後の場所のみを置き換えます。反復ごとに変数を更新していません。

于 2010-12-22T23:37:23.383 に答える
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;?>
于 2010-12-23T00:22:01.177 に答える
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);  
        }
    }
}
于 2010-12-23T06:57:51.030 に答える
0

foreach の反復ごとに再割り当てされる $build をエコーし​​ます。

代わりにこれを書くべきだった

 $template = str_replace('{' . $brace . '}', $replacement, $template);
于 2010-12-22T23:39:47.203 に答える