【问题标题】:How to make a php template engine?如何制作一个php模板引擎?
【发布时间】:2011-07-29 06:57:11
【问题描述】:

我需要制作一个小而简单的 php 模板引擎,我搜索了很多,其中很多都太复杂而无法理解,我不想使用 smarty 和其他类似的引擎,我从 Stack Overflow 得到了一些想法,比如这个:

$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

【问题讨论】:

  • 为什么不想使用 Smarty?与其他模板引擎相比,您的方法有哪些优势?
  • 为什么要重新发明轮子?如果这只是一个案例,您不想花时间学习聪明,那么我认为您犯了一个错误。从长远来看,这将为您节省时间,因为 smarty 开发人员已经解决了您在尝试创建自己的引擎时会遇到的所有问题。
  • PHP 已经是一个模板引擎。 Smarty 正在重新发明轮子。
  • "PHP 已经是一个模板引擎。"从最严格的意义上说,这是真的,但在某些时候它非常冗长和不灵活。

标签: php html arrays templates


【解决方案1】:

文件.html:

<html>

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

</html>

文件.php:

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

没有比这更简单的了。是的,它使用全局变量,但如果游戏名称很简单,就是这样。只需访问“http://example.com/file.php”即可。

现在,如果您希望用户在浏览器的地址栏中看到“file.html”,您必须配置您的网络服务器以将 .html 文件视为 PHP 脚本,这有点复杂,但绝对可行.完成后,您可以将两个文件合并为一个文件:

文件.html:

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

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

</html>

【讨论】:

  • 如果可以的话,我会给+100,没有必要重新发明轮子。 PHP的意思是PHP超文本处理器,“超文本”不存在是误...
  • 您想要一个模板引擎来使您的模板可模板化吗?
  • 一百次是的。 PHP 是一个模板引擎。启用短标签,这不是一个糟糕的模板引擎。
  • 我知道,但我只是想让 php 开发人员的 php 部分和 html 开发人员的 html 部分分开,并在需要时轻松更改主题
【解决方案2】:

如果为了更易于维护的脚本,将它们移到函数中会怎样?

类似这样的:

<?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);

【讨论】:

    【解决方案3】:

    一旦你解决了所有的错误,解决了你自己陷入的巨大的性能问题,你最终会得到像 Smarty 和其他人一样的模板引擎。

    这种 find'n'replace 方法比编译到 PHP 慢得多。它不能很好地处理转义(你会遇到 XSS 问题)。添加条件和循环将是相当困难的,您迟早会需要它们。

    【讨论】:

      【解决方案4】:
          <?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();
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多