嵌套模板的简单 PHP 模板类

Jas*_*vis 1 php templates

我想构建和使用一个超级简单的 PHP 模板类。我不需要支持任何自定义标签或任何东西。

只需要能够从类外部分配变量,然后在模板文件内部解析它们。

然后我需要能够将解析后的 H​​TML 输出返回给一个变量。

另一个不错的功能是能够将模板文件相互嵌套,我很久以前就看到过,而且实现起来非常简单。

我无法访问我的任何旧模板类 PHP 代码,因此我可以使用任何帮助来构建一个轻量级、快速且非常简单的类来执行上述这些任务。我不想使用现有的模板系统!

从理论上讲,我认为它应该与此类似...

//Instantiate a Template object
$template = new Template("path/to/template.php");

// Set some Template variables that can be accessed inside the template file
$template->title = "Hello world!";
$template->description = "This is a ridiculously simple template";
$template->posts = array();

// Return the processed template file to a variable
$outputHtml = $template->render();
Run Code Online (Sandbox Code Playgroud)

模板文件本身可能是这样的......

<html>
    <head>
        // Header scripts and content
    </head>

    <body>
        // Body content
        <h1><?php echo $this->title; ?></h1>

        <div><?php echo $this->description; ?></div>

        <?php
        print_r($this->posts);
        foreach($this->posts as $key => $value){
            echo $key;
            echo '<br>';
            echo $key;
        }
        ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

一个 PHP 模板类可能看起来像这样......

class Template
{
    protected $template;
    protected $variables = array();

    public function __construct($template)
    {
        $this->template = $template;
    }

    public function __get($key)
    {
        return $this->variables[$key];
    }

    public function __set($key, $value)
    {
        $this->variables[$key] = $value;
    }

    public function render()
    {
        extract($this->variables);
        chdir(dirname($this->template));
        ob_start();

        include basename($this->template);

        return ob_get_clean();
    }
}
Run Code Online (Sandbox Code Playgroud)

那么我可以做些什么来改善这一点?还要在模板内嵌套模板以更好地工作?

小智 5

我知道这是 2014 年的帖子,但我在模板类方面遇到了同样的问题,这是我解决问题的方法:

这是控制器:index.php

<?php require 'core/init.php' ;

// Get template and assign vars
$template = new Template('templates/frontpage.php');

// Assign Vars
$template->title = "Hello world!";
$template->description = "This is a ridiculously simple template";

echo $template;
Run Code Online (Sandbox Code Playgroud)

模板类:Template.php

<?php
/*
 *  Template Class
 *  Creates a template/view object
 */
class Template {
    //Path to template
    protected $template;
    //Variables 
    protected $vars = array();

    /*
     * Class Constructor
     */
    public function __construct($template){
        $this->template = $template;
    }

    /* __get() and __set() are run when writing data to inaccessible properties.
     * Get template variables
     */
    public function __get($key){
        return $this->vars[$key];
    }

    /*
     * Set template variables
     */
    public function __set($key, $value){
        $this->vars[$key] = $value;
    }

    /*
     * Convert Object To String
     */
    public function __toString(){
        extract($this->vars); // extract our template variables ex: $value
        // print_r($this->vars ) ;  testing
        chdir(dirname($this->template));
        ob_start(); // store as internal buffer

        include basename($this->template);  // include the template into our file

        return ob_get_clean();
    }
}
Run Code Online (Sandbox Code Playgroud)

视图: frontpage.php

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
    <link rel="stylesheet" type="text/css" media="screen" href="css/master.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
  <?php echo title ; ?> 
  <?php echo description ; ?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)