[ACCEPTED]-How to pass parameters to PHP template rendered with 'include'?-include
Consider including a PHP file as if you 5 were copy-pasting the code from the include 4 into the position where the include-statement 3 stands. This means that you inherit the 2 current scope.
So, in your case, $param is already 1 available in the given template.
$param should be already available inside 12 the template. When you include() a file 11 it should have the same scope as where it 10 was included.
from http://php.net/manual/en/function.include.php
When a file is included, the 9 code it contains inherits the variable 8 scope of the line on which the include occurs. Any 7 variables available at that line in the 6 calling file will be available within 5 the called file, from that point forward. However, all functions 4 and classes defined in the included file 3 have the global scope.
You could also do 2 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 1 still called $row.
I use the following helper functions when 5 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 4 the template to the screen directly. render 3 will return it as a string. It makes use 2 of ob_get_contents to return the printed 1 output of a function.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.