2017-05-31 159 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');