目前我这样做:
function load_template($script, $args){
extract($args);
require __DIR__ . '/templates/' . $script;
}
在我的控制器代码中:
// if home page was requested
load_template('home.php', array(
'title' => get_title(),
'content' => get_content(),
...
));
该模板只是一个PHP脚本
<?php echo $title; ?>...
我想知道是否有可能以某种方式延迟加载这些变量,所以我实际上并没有运行get_title()或get_content(),直到模板专门请求变量.
如果没有创建模板解析器,这可能是可能的吗?我真的很想坚持使用简单的.php脚本和html作为模板.
简而言之,我要问的是,是否可以仅在首次请求时自动为变量赋值.
$var = func(); // this should not run
if($var){ // now the code above should run:)
echo $var; //
}