2011-04-04 46 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