doujiku1028 2013-08-07 23:40
浏览 21

str_replace和函数的模板问题

I am using the following to replace curly bracket markers in .tpl files, sort of a quasi templating engine. I do not really want the overhead of having a template engine when I just need it for these three simple things. The problem I am having is that when I call a function that generates for example a nav (buildNav), when the page renders they for some reason parse at the top of the page, and not within the curly bracket area where they should be. What is going on and how can I fix it? For reference I am referring to the functions in the array of body.

<?php
class Template
{
    public $path;
    public $ext = '.tpl';
    public function __construct()
    {
        $this->path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
    }
    public function head($title = '')
    {
        $title = isset($title) ? $title : 'NEOU CMS';
        $replace = array(
            "page_title" => $title,
            "JQUERY_UI_CSS" => JQUERY_UI_CSS,
            "JQUERY_JS" => JQUERY_JS,
            "JQUERY_UI_JS" => JQUERY_UI_JS
        );
        echo self::parse_template('header', $replace);
    }
    public function body()
    {
        require('customize.php');
        $image = new Images();
        $replace = array(
            "profile_picture" => $image->profilePic(),
            "navigation" => buildNav()
        );
        echo self::parse_template('body', $replace);
    }
    public function footer()
    {
        $replace = array(
            "date" => date("Y")
        );
        echo self::parse_template('footer', $replace);
    }
    /**
     * @param string $filename
     * @param array $data
     */
    public function parse_template($filename, $data)
    {
        $out = file_get_contents($this->path . $filename . $this->ext);
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                $out = str_replace('{' . $key . '}', $value, $out);
            }
        }
        return $out;
    }
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号