2011-04-04 45 views
5

我需要做一個小而簡單的PHP模板引擎我搜索了很多,其中許多太複雜,不能理解,我不想使用smarty和其他類似的引擎,我已經從Stack Overflow中得到了一些想法,像這樣:如何製作一個php模板引擎?

$template = file_get_contents('file.html'); 
$array = array('var1' => 'value', 
       'txt' => 'text'); 

foreach($array as $key => $value) 
{ 
    $template = str_replace('{'.$key.'}', $value, $template); 
} 

echo $template; 

現在不是呼應了模板我只想補充包括「file.html」,它會顯示與正確的變量值的文件,我想把引擎在一個單獨的地方,只是包括它在模板中我想用它來聲明數組,並在最後包含像phpbb這樣的html文件。對不起,我問很多,但任何人都可以解釋這背後的基本概念?

編輯:好吧,讓我坦白我正在製作一個論壇腳本,我有噸的想法,但我想讓它的模板系統像phpbb所以我需要一個單獨的模板引擎自定義一個,如果你可以幫助,然後請你被邀請和我一起工作。對不起,廣告..:p

+0

你爲什麼不想要使用Smarty?你的方法比其他模板引擎有什麼優勢? – mdm 2011-04-04 15:38:42

+0

爲什麼重新發明輪子?如果它只是一個案例,你不想投入時間學習聰明,那麼我認爲你犯了一個錯誤。從長遠來看,它將爲您節省時間,因爲聰明的開發人員已經解決了嘗試創建自己的引擎時遇到的所有問題。 – 3urdoch 2011-04-04 15:41:00

+14

PHP已經是一個模板引擎。 Smarty正在重塑車輪。 – Capsule 2011-04-04 15:44:46

回答

5

如果,易於維護的腳本,您將那些職能是什麼?

是這樣的:

<?php 

function get_content($file, $data) 
{ 
    $template = file_get_contents($file); 

    foreach($data as $key => $value) 
    { 
    $template = str_replace('{'.$key.'}', $value, $template); 
    } 

    return $template; 
} 

你也可以使用這種方式:

<?php 

$file = '/path/to/your/file.php'; 
$data = = array('var1' => 'value', 
       'txt' => 'text'); 

echo get_content($file, $data); 
2

一旦你消除了所有的bug,修復了你陷入的巨大性能問題,你將最終得到模板引擎,就像Smarty和其他。

這種find'n'replace方法比彙編到PHP要慢得多。它不能很好地處理轉義(你會遇到XSS問題)。添加條件和循環相當困難,遲早你會需要它們。

11

file.html:

<html> 

<body> 
<h3>Hi there, <?php echo $name ?></h3> 
</body> 

</html> 

file.php:

<?php 
    $name = "Keshav"; 
    include('file.html'); 
?> 

沒有什麼比這更簡單。是的,它使用全局變量,但如果簡單是遊戲的名稱,就是這樣。只需訪問「http://example.com/file.php」即可。

現在,如果您希望用戶在瀏覽器的地址欄中看到'file.html',則必須將您的網絡服務器配置爲將.html文件視爲PHP腳本,這有點複雜,但絕對可行的。一旦這樣做了,你可以將這兩個文件合併成一個單一的一個:

file.html:

<?php 
    $name = "Keshav"; 
?> 
<html> 

<body> 
<h3>Hi there, <?php echo $name ?></h3> 
</body> 

</html> 
+2

如果可以的話,我會給+100,不需要重新發明輪子。 PHP意味着PHP超文本處理器,並且「超文本」不會出錯...... – Capsule 2011-04-04 15:43:34

+0

您想使用模板引擎來使模板成爲模板嗎? – 2011-04-04 15:50:13

+2

百倍的是。 ** PHP是一個模板引擎**。啓用短標籤並且它不是一個糟糕的模板引擎。 – meagar 2011-04-04 15:53:07

1
<?php 
    class view { 
     private $file; 
     private $vars = array(); 

     public function __construct($file) { 
      $this->file = $file; 
     } 

     public function __set($key, $val) { 
      $this->vars[$key] = $val; 
     } 

     public function __get($key, $val) { 
      return (isset($this->vars[$key])) ? $this->vars[$key] : null; 
     } 

     public function render() { 
      //start output buffering (so we can return the content) 
      ob_start(); 
      //bring all variables into "local" variables using "variable variable names" 
      foreach($this->vars as $k => $v) { 
       $$k = $v; 
      } 

      //include view 
      include($this->file); 

      $str = ob_get_contents();//get teh entire view. 
      ob_end_clean();//stop output buffering 
      return $str; 
     } 
    } 

這裏是如何使用它:

<?php 
    $view = new view('userprofile.php'); 
    $view->name = 'Afflicto'; 
    $view->bio = "I'm a geek."; 
    echo $view->render(); 
+0

你需要給它信用所屬! http://www.sitepoint.com/flexible-view-manipulation-1/ – goodMan 2013-11-01 16:52:45

+0

@goodMan,哈哈我從來沒有見過那篇文章。 – 2014-04-05 10:31:47