简单的PHP模板类

时间:2016-11-15 14:29:14

标签: php class templates

我想从PHP中分离我的HTML,所以在搜索之后我发现了一个很棒的小PHP类就可以了。只发布我尝试将两个模板合并在一起,但它不起作用。

以下是我从以下网站

找到的原始课程

http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WCsa8CTy2ng

class Template {
    /**
     * The filename of the template to load.
     *
     * @access protected
     * @var string
     */
    protected $file;

    /**
     * An array of values for replacing each tag on the template (the key for each value is its corresponding tag).
     *
     * @access protected
     * @var array
     */
    protected $values = array();

    /**
     * Creates a new Template object and sets its associated file.
     *
     * @param string $file the filename of the template to load
     */
    public function __construct($file) {
        $this->file = $file;
    }

    /**
     * Sets a value for replacing a specific tag.
     *
     * @param string $key the name of the tag to replace
     * @param string $value the value to replace
     */
    public function set($key, $value) {
        $this->values[$key] = $value;
    }

    /**
     * Outputs the content of the template, replacing the keys for its respective values.
     *
     * @return string
     */
    public function output() {
        /**
         * Tries to verify if the file exists.
         * If it doesn't return with an error message.
         * Anything else loads the file contents and loops through the array replacing every key for its value.
         */
        if (!file_exists($this->file)) {
            return "Error loading template file ($this->file).<br />";
        }
        $output = file_get_contents($this->file);

        foreach ($this->values as $key => $value) {
            $tagToReplace = "[@$key]";
            $output = str_replace($tagToReplace, $value, $output);
        }

        return $output;
    }

    /**
     * Merges the content from an array of templates and separates it with $separator.
     *
     * @param array $templates an array of Template objects to merge
     * @param string $separator the string that is used between each Template object
     * @return string
     */
    static public function merge($templates, $separator = "\n") {
        /**
         * Loops through the array concatenating the outputs from each template, separating with $separator.
         * If a type different from Template is found we provide an error message. 
         */
        $output = "";

        foreach ($templates as $template) {
            $content = (get_class($template) !== "Template")
                ? "Error, incorrect type - expected Template."
                : $template->output();
            $output .= $content . $separator;
        }

        return $output;
    }
}

有效的代码

$post = new Template("post.tpl");
$post->set("post_title", $post_title);
$post->set("post_description", $post_description);
$post->set("content", $post->output());
echo $post->output();

即使我想循环,如果我添加代码它工作正常。但后来我尝试将两个模板文件合并在一起

all_posts.tpl

<div class=”posts”>
<h1>[@page_title]</</h1>
[@display_posts]
</div>
display_posts.tpl
<div class=”post”>

<h2>[@display_title]</h2>

<p>[@display_description]</p>

</div>

所以我现在要做的是将display_posts.tpl推送到all_posts.tpl并替换标签[@display_posts]

所以在我的PHP中,我在下面做了

$post = new Template("all_posts.tpl ");
$post->set("page_title", "All Posts");
echo $post->output();

//created a array from a mysqli loop

$post_set  = array();

while ($post_row = mysqli_fetch_array($posts)){

    $post_title = $post_row['title'];
    $post_description = $post_row['description'];

    $post_set[] = array(
        "display_title" => $post_title, 
        "display_description" => $post_description
);

}



foreach ($post_set as $posts) {
        $row = new Template("list_users_row.tpl");

        foreach ($posts as $key => $value) {
            $row->set($key, $value);
        }
        $postsTemplates[] = $posts;
    }

// here i try to merge template 

$postsContents = Template::merge($postsTemplates);

$layout->set("content", $postsContents->output());

echo $layout->output();

但这是抛出一个错误集()不是一个函数。有人可以帮我解决这个问题吗?我还在学习php课程。

1 个答案:

答案 0 :(得分:1)

$ layout-&gt; set()不起作用的原因是$ layout不是模板对象。在您的代码中,您使用 $ post = new Template(“all_posts.tpl”); 。这使得$ post一个Template对象可以使用Template类中的set()函数。

所以你应该这样做:$ layout = new Template(....)而且你可以调用$ layout-&gt; set()。

在教程中他们也这样做:

include("template.class.php");
$profile = new Template("user_profile.tpl");
$profile->set("username", "monk3y");
$profile->set("photoURL", "photo.jpg");
$profile->set("name", "Monkey man");
$profile->set("age", "23");
$profile->set("location", "Portugal");

$layout = new Template("layout.tpl");
$layout->set("title", "User profile");
$layout->set("content", $profile->output());

echo $layout->output();

希望这有帮助。