【发布时间】:2013-05-14 13:45:48
【问题描述】:
目前我正在这样做:
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 脚本
<!DOCTYPE html>
<html>
<head>
<title> <?php echo $title; ?> </titlee>
...
我想知道是否可以以某种方式延迟加载这些变量,所以在模板明确请求变量之前,我实际上不会运行 get_title() 或 get_content()。
如果不创建模板解析器,这可能吗?我真的很想坚持使用简单的 .php 脚本和 html 作为模板。
简而言之,我要问的是是否可以仅在第一次请求变量时自动为其赋值。
$var = func(); // this should not run
if($var){ // now the code above should run:)
echo $var; // <- the value that was just assigned (don't run func() again)
}
【问题讨论】:
标签: php templates lazy-loading