Name
templateExists() — 检查模板是否存在
说明
bool templateExists(string template);
检查的模板可以指定文件路径,或者一个模板资源。
Example 14.48. templateExists()
下面例子使用$_GET['page']
来{include}
指定的模板。如果模板不存在,则会显示一个“page not found”的错误信息。首先是page_container.tpl
的模板内容:
- <html>
- <head><title>{$title}</title></head>
- <body>
- {include file='page_top.tpl'}
- {* 包含中间内容的模板 *}
- {include file=$content_template}
- {include file='page_footer.tpl'}
- </body>
然后在PHP脚本中:
- <?php
- // 设置文件名,如 index.inc.tpl
- $mid_template = $_GET['page'].'.inc.tpl';
- if( !$smarty->templateExists($mid_template) ){
- $mid_template = 'page_not_found.tpl';
- }
- $smarty->assign('content_template', $mid_template);
- $smarty->display('page_container.tpl');
- ?>
参见 display()
, fetch()
, {include}
和 {insert}
原文: https://www.smarty.net/docs/zh_CN/api.template.exists.tpl