Name

display() — 显示

说明

void display(string template,
string cacheid,
string compile_id);

显示模板。如果希望返回而并非显示当前模板的内容,请使用fetch()。 该函数需要指定一个合法的模板资源的类型和路径。 第二个可选的参数
$cacheid设置缓存,详情参见 缓存

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()

  1. <?php
  2. include(SMARTY_DIR.'Smarty.class.php');
  3. $smarty = new Smarty();
  4. $smarty->setCaching(true);
  5.  
  6. // 仅在缓存不存在的时候调用
  7. if(!$smarty->isCached('index.tpl')) {
  8.  
  9. // 数据
  10. $address = '245 N 50th';
  11. $db_data = array(
  12. 'City' => 'Lincoln',
  13. 'State' => 'Nebraska',
  14. 'Zip' => '68502'
  15. );
  16.  
  17. $smarty->assign('Name', 'Fred');
  18. $smarty->assign('Address', $address);
  19. $smarty->assign('data', $db_data);
  20.  
  21. }
  22.  
  23. // 显示
  24. $smarty->display('index.tpl');
  25. ?>
  26.  


Example 14.20. 其他 display() 的示例

模板资源的方式来显示不在$template_dir目录下的模板。

  1. <?php
  2. // 绝对路径
  3. $smarty->display('/usr/local/include/templates/header.tpl');
  4.  
  5. // 绝对路径(使用file://)
  6. $smarty->display('file:/usr/local/include/templates/header.tpl');
  7.  
  8. // windows环境的绝对路径(务必是file:开头)
  9. $smarty->display('file:C:/www/pub/templates/header.tpl');
  10.  
  11. // 来自模板资源库db
  12. $smarty->display('db:header.tpl');
  13. ?>
  14.  

参见 fetch()templateExists().

原文: https://www.smarty.net/docs/zh_CN/api.display.tpl