模板类中的PHP

模板类中的PHP,php,class,templates,Php,Class,Templates,我目前正在从broculus开发一个模板类。 现在我想使用类将PHP安装到模板中。不幸的是,我现在失败了。 据估计,这肯定不是我所确定的。现在我想问你,你是否知道我如何轻松解决这个问题 链接: 在这段代码中,我想得到一个变量的值,这是php代码。 应执行该守则 例如: $row_1->content='$name='Pagetitle';echo$name;' $row->content包含php和完整的脚本 $layout = new Template("template/layout.tpl

我目前正在从broculus开发一个模板类。 现在我想使用类将PHP安装到模板中。不幸的是,我现在失败了。 据估计,这肯定不是我所确定的。现在我想问你,你是否知道我如何轻松解决这个问题

链接:

在这段代码中,我想得到一个变量的值,这是php代码。 应执行该守则

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

$row->content包含php和完整的脚本

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

/**
 * 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;
    }
}
我用ob_start找到了一个解决方案。在panel.php中,我通过eval执行php代码

你觉得这个怎么样

这是一场战争,keine wirkliche Lösung,她戴着一顶帽子。 Versuche nun模具模板引擎von Smarty zu nutzen。我的PHP im模板是什么

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');

对不起,不清楚你在问什么。你有密码吗?也许你可以用德语添加你的问题,我来翻译它?Danke für deine Hilfe。现在看看类和方法集:这种使用eval和执行php代码的方法是行不通的,无论如何这是个坏主意。Eval是邪恶的Eval是teuflisch。请找到另一种方法去做你想做的事情。他是一个艺术家,他是一个艺术家。所以我不知道该怎么做。
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');