Name

templateExists() — 检查模板是否存在

说明

bool templateExists(string template);
检查的模板可以指定文件路径,或者一个模板资源。


Example 14.48. templateExists()

下面例子使用$_GET['page']{include}指定的模板。如果模板不存在,则会显示一个“page not found”的错误信息。首先是page_container.tpl的模板内容:

  1. <html>
  2. <head><title>{$title}</title></head>
  3. <body>
  4. {include file='page_top.tpl'}
  5.  
  6. {* 包含中间内容的模板 *}
  7. {include file=$content_template}
  8.  
  9. {include file='page_footer.tpl'}
  10. </body>
  11.  

然后在PHP脚本中:

  1. <?php
  2.  
  3. // 设置文件名,如 index.inc.tpl
  4. $mid_template = $_GET['page'].'.inc.tpl';
  5.  
  6. if( !$smarty->templateExists($mid_template) ){
  7. $mid_template = 'page_not_found.tpl';
  8. }
  9. $smarty->assign('content_template', $mid_template);
  10.  
  11. $smarty->display('page_container.tpl');
  12.  
  13. ?>
  14.  

参见 display(), fetch(), {include}{insert}

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