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

前端 未结 3 1612
醉话见心
醉话见心 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 08:06

    $param should be already available inside the template. When you include() a file it should have the same scope as where it was included.

    from http://php.net/manual/en/function.include.php

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

    You could also do something like:

    print render("/templates/blog_entry.php", array('row'=>$row));
    
    function render($template, $param){
       ob_start();
       //extract everything in param into the current scope
       extract($param, EXTR_SKIP);
       include($template);
       //etc.
    

    Then $row would be available, but still called $row.

提交回复
热议问题