What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
display() — 显示
void display(string template,
string cache_id,
string compile_id);
显示模板。如果希望返回而并非显示当前模板的内容,请使用fetch()
。
该函数需要指定一个合法的模板资源的类型和路径。
第二个可选的参数$cache_id
设置缓存,详情参见
缓存。
As an optional third parameter, you can pass a
$compile_id
.
This is in the event that you want to compile different versions of
the same template, such as having separate templates compiled
for different languages. You can
also set the
$compile_id
variable once instead of passing
this to each call to this function.
Example 14.19. display()
<?php include(SMARTY_DIR.'Smarty.class.php'); $smarty = new Smarty(); $smarty->setCaching(true); // 仅在缓存不存在的时候调用 if(!$smarty->isCached('index.tpl')) { // 数据 $address = '245 N 50th'; $db_data = array( 'City' => 'Lincoln', 'State' => 'Nebraska', 'Zip' => '68502' ); $smarty->assign('Name', 'Fred'); $smarty->assign('Address', $address); $smarty->assign('data', $db_data); } // 显示 $smarty->display('index.tpl'); ?>
Example 14.20. 其他 display() 的示例
用模板资源的方式来显示不在
$template_dir
目录下的模板。
<?php // 绝对路径 $smarty->display('/usr/local/include/templates/header.tpl'); // 绝对路径(使用file://) $smarty->display('file:/usr/local/include/templates/header.tpl'); // windows环境的绝对路径(务必是file:开头) $smarty->display('file:C:/www/pub/templates/header.tpl'); // 来自模板资源库db $smarty->display('db:header.tpl'); ?>
参见 fetch()
和
templateExists()
.