【问题标题】:How to implement display like smarty in PHP?如何在 PHP 中实现类似 smarty 的显示?
【发布时间】:2010-12-18 04:30:20
【问题描述】:
$smarty->assign('name',$value);
$smarty->display("index.html");

这样它会自动替换 index.html 中的$variables 从而节省大量的echo

【问题讨论】:

  • @Mask:一些用户已经在你的最后一个问题上实现了这一点......
  • 你为什么不用 smarty 的display
  • @RC:在我看来,他正在重新编写 Smarty ...
  • @RageZ,根据您指出的问题中的回复,我也看到了这一点,但我不明白为什么..

标签: php smarty templating


【解决方案1】:

你可以这样使用:

// assigns the output of a file into a variable...
function get_include_contents($filename, $data='') {
    if (is_file($filename)) {
        if (is_array($data)) {
            extract($data);
        }
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}


$data = array('name'=>'Ross', 'hobby'=>'Writing Random Code');
$output = get_include_contents('my_file.php', $data);
// my_file.php will now have access to the variables $name and $hobby

【讨论】:

    【解决方案2】:

    取自上一个问题

    class Templater {
    
        protected $_data= array();
    
        function assign($name,$value) {
          $this->_data[$name]= $value;
        }
    
        function render($template_file) {
           extract($this->_data);
           include($template_file);
        }
    }
    
    $template= new Templater();
    $template->assign('myvariable', 'My Value');
    $template->render('path/to/file.tpl');
    

    在模板中

    <?= $foobar ?>
    

    将打印 foobar .... 如果您需要创建自己的语法,您可以使用 preg_replace_callback

    例如:

    function replace_var($matches){
        global $data;
        return $data[$matches[1]];
    }
    preg_replace_callback('/{$([\w_0-9\-]+)}/', 'replace_var');
    

    【讨论】:

    【解决方案3】:

    使用 previous answer 中的 Templater 类,您可以更改渲染函数以使用正则表达式

    function render($template_file) {
      $patterns= array();
      $values= array();
      foreach ($this->_data as $name=>$value) {
        $patterns[]= "/\\\$$name/";
        $values[]= $value;
      }
      $template= file_get_contents($template_file);
      echo preg_replace($patterns, $values, $template);
    }
    
    ......
    
    $templater= new Templater();
    $templater->assign('myvariable', 'My Value');
    $templater->render('mytemplate.tpl');
    

    还有以下模板文件:

    <html>
    <body>
    This is my variable <b>$myvariable</b>
    </body>
    </html>
    

    将呈现:

    这是我的变量我的价值

    免责声明:实际上并没有运行它来查看它是否有效!请参阅 preg_replace 上的 PHP 手册,示例 #2:http://php.net/manual/en/function.preg-replace.php

    【讨论】:

      【解决方案4】:

      你描述的功能由extractphp函数处理,例如:

      // Source: http://www.php.net/manual/en/function.extract.php
      $size = "large";
      $var_array = array("color" => "blue", "size"  => "medium", "shape" => "sphere");
      extract($var_array, EXTR_PREFIX_SAME, "wddx");
      echo "$color, $size, $shape, $wddx_size\n";
      

      但我强烈建议您使用 Sergey 或 RageZ 发布的类之一,因为否则您将重新发明轮子,PHP 中有很多低调和高端模板类,实际上有很多 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-04
        • 2012-08-31
        • 1970-01-01
        • 2018-04-13
        • 2010-11-04
        • 1970-01-01
        • 1970-01-01
        • 2010-12-04
        相关资源
        最近更新 更多