简体   繁体   English

模板类中的PHP

[英]PHP in Template Class

I am currently working on a template class from broculus. 我目前正在开发来自Broculus的模板类。 Now I wanted to install PHP into the template using the class. 现在,我想使用该类将PHP安装到模板中。 Unfortunately, I am currently failing. 不幸的是,我目前失败了。 By eval it is certainly not I have established. 显然,我还没有确定。 Now I wanted to ask if you have an idea how I can solve the problem easily. 现在,我想问一下您是否对如何轻松解决问题有所了解。

Link: http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WS8jfWjyjIX 链接: http//www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WS8jfWjyjIX

In that code I want to get the value of a variable, which is php code. 在该代码中,我想获取变量的值,即php代码。 That code shall be executed. 该代码将被执行。

Example: $row_1->content = '$name = 'Pagetitle'; echo $name;'; 示例: $row_1->content = '$name = 'Pagetitle'; echo $name;'; $row_1->content = '$name = 'Pagetitle'; echo $name;';

$row->content contains php and complete scripts. $row->content包含php和完整的脚本。

$layout = new Template("template/layout.tpl");
$layout->set("title", $sitename);
$layout->set("content", eval($row_1->content));

Class: 类:

/**
 * Simple template engine class (use [@tag] tags in your templates).
 * 
 * @link http://www.broculos.net/ Broculos.net Programming Tutorials
 * @author Nuno Freitas <nunofreitas@gmail.com>
 * @version 1.0
 */
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;
    }
}

I found a solution using ob_start . 我使用ob_start找到了解决方案。 In the panel.php I execute the PHP code via eval . panel.php我通过eval执行PHP代码。

// Ausgabepuffer an
ob_start();
// Skript ausführen
$_GET['panel'] = 1;
include('panel.php');
// Ausgabe aus Puffer holen
$inhalt = ob_get_contents();
// Puffer schließen
ob_end_clean();

//Layout holen
$layout = new Template("template/layout.tpl");
$layout->set("title", $sitename);
$layout->set("content", $inhalt);

What do you think about this? 你怎么看待这件事?

Es war keine wirkliche Lösung, wie es sich herausgestellt hat. Es war keine wirklicheLösung,戴帽子。 Versuche nun die Template Engine von Smarty zu nutzen. Versuche修女模板引擎von Smarty zu nutzen。 Weiß jemand wie ich dort PHP im Template darstellen kann? 更多信息PHP im Template darstellen kann吗?

require '../libs/Smarty.class.php';

$smarty = new Smarty;

$smarty->compile_check = true;
$smarty->debugging = false;

$smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill");
$smarty->assign("Menu","MENU");
$smarty->assign("Content", "echo 'Testinhalt soll angezeigt werden.'; ");


$smarty->display('index.tpl');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM