Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套模板的简单PHP模板类_Php_Templates - Fatal编程技术网

嵌套模板的简单PHP模板类

嵌套模板的简单PHP模板类,php,templates,Php,Templates,我想构建并使用一个超级简单的PHP模板类。我不需要支持任何自定义标记或任何东西 只需要能够从类外部分配变量,然后在模板文件内部解析它们 然后,我需要能够将解析后的HTML输出返回到一个变量 另一个很好的特性是能够将模板文件嵌套在另一个模板文件中,我很久以前就看到了这一点,而且实现起来非常简单 我没有访问任何旧模板类PHP代码的权限,因此我可以使用任何帮助来构建一个轻量级、快速且非常简单的类来完成上述任务。我不想使用现有的模板系统 理论上,我相信它应该起到类似的作用 //Instantiate a

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

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

然后,我需要能够将解析后的HTML输出返回到一个变量

另一个很好的特性是能够将模板文件嵌套在另一个模板文件中,我很久以前就看到了这一点,而且实现起来非常简单

我没有访问任何旧模板类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();
模板文件本身可能如下所示

<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>

那么我能做些什么来改进这个呢?还可以使模板内部的模板嵌套工作得更好?

我认为这是设计PHP模板的更好方法

我认为这是设计PHP模板的更好方法

我知道这是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;
<?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();
    }
}
<!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>

我知道这是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;
<?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();
    }
}
<!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>


谢谢分享。只是想确认一下,与我的原始版本相比,唯一的变化是使用了PHP魔术方法
\uu toString()
是否正确?是的,toString可以将对象打印为字符串。感谢分享。只是想确认一下,与我的原始版本相比,唯一的变化是使用了PHP魔术方法
\uu toString()
是否正确?是的,toString可以将对象打印为字符串。