【问题标题】:PHP in Template Class模板类中的 PHP
【发布时间】:2017-11-01 20:29:23
【问题描述】:

我目前正在研究 broculus 的模板类。 现在我想使用该类将 PHP 安装到模板中。不幸的是,我目前失败了。 通过 eval 它肯定不是我建立的。现在我想问一下您是否知道如何轻松解决问题。

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

在该代码中,我想获取一个变量的值,即 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;
    }
}

【问题讨论】:

  • 抱歉,不清楚您在问什么。你有代码吗?也许你用德语添加你的问题,我会翻译它?
  • Danke für deine Hilfe.
  • 现在看到类和方法set(): 这个东西eval 并在那里执行php 代码是行不通的,无论如何都是个坏主意。评估是邪恶的(“评估”ist teuflisch)。请找到另一种方式来做你想要实现的目标。 Du solltest eine andere art finden das zu erreichen was du willst。 So wird es nicht gehen.

标签: php class templates


【解决方案1】:

我找到了使用ob_start 的解决方案。在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);

您对此有何看法?

Es war keine wirkliche Lösung,wie es sich herausgestellt 帽子。 Versuche 修女模版引擎 von Smarty zu nutzen。 Weiß jemand wie ich dort 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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 2023-03-16
    • 2011-02-12
    • 2010-11-08
    相关资源
    最近更新 更多