How to pass parameters to PHP template rendered with 'include'?

前端 未结 3 1613
醉话见心
醉话见心 2021-02-01 07:32

need your help with PHP templating. I\'m new to PHP (I\'m coming from Perl+Embperl). Anyway, my problem is simple:

  • I have a small template to render some item, let
3条回答
  •  借酒劲吻你
    2021-02-01 07:48

    I use the following helper functions when I work on simple websites:

    function function_get_output($fn)
    {
      $args = func_get_args();unset($args[0]);
      ob_start();
      call_user_func_array($fn, $args);
      $output = ob_get_contents();
      ob_end_clean();
      return $output;
    }
    
    function display($template, $params = array())
    {
      extract($params);
      include $template;
    }
    
    function render($template, $params = array())
    {
      return function_get_output('display', $template, $params);
    }
    

    display will output the template to the screen directly. render will return it as a string. It makes use of ob_get_contents to return the printed output of a function.

提交回复
热议问题