Smarty快速入门之二
为了以后更方便的使用smarty,我们可以将“加载Smarty 模版引擎”、“建立 Smarty 对象”、“设定Smarty 对象的参数”这三步放到一个公共的php文件内,以后在需要使用的地方我们直接reuqire一下,即可,例如:
1. 建立一个main.php
[code lang="php"]
template_dir = ROOT.templates.DIR_SEP;
$tpl->complie_dir = ROOT.templates_c.DIR_SEP;
$tpl->config_dir = ROOT.configs.DIR_SEP;
$tpl->cache_dir = ROOT.cache.DIR_SEP;
$tpl->left_delimiter = <{;
$tpl->right_delimiter = }>;
?>
[/code]
注:left_delimiter和right_delimiter为左右结束符变量,此处可定义为其他字符串(默认为{})。
2. 在templates下,新建立一个test.htm
[code lang="php"]
<{$content}>
[/code]
3. 调用模板页给title和content填充内容,新建一个test_smarty_1.php
[code lang="php"]
assign(title, New Message);
$tpl->assign(content, This is test smarty!);
$tpl->display(test.htm);
?>
[/code]
也可以这样写
[code lang="php"]
assign(array(title=>NewMessage, content=>Thi
s is test smarty!));
$tpl->display(test.htm);
?>
[/code]
输出结果:
页面title显示:NewMessage
页面内容显示:This is test smarty!