2017-05-31 137 views
0

我目前正在研究broculus的模板类。 现在我想使用该类将PHP安装到模板中。不幸的是,我目前失败了。 通过评估它肯定不是我已经建立。现在我想问你是否有一个想法可以轻松解决问题。模板类中的PHP

链接: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 <[email protected]> 
* @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; 
    } 
} 
+0

对不起,你不清楚你在问什么。你有任何代码吗?也许你用德语添加你的问题,我会翻译它? – Jeff

+0

Dankefürdeine Hilfe。 –

+0

现在看到类和mothod'set()':这个用'eval'并且在那里执行php代码的东西是行不通的,反正也是个坏主意。 Eval是邪恶的(“Eval”ist teuflisch)。请找到另一种做你想达到的方式。 Du Solltest eine andere art finden das zu erreichen was du willst。所以,你好。 – Jeff

回答

-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 wirklicheLösung,wie es sich herausgestellt hat。 Versuche nun die Template Engine 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');