template_exists()
template_exists() -- 检查一特定模板是否存在
描述
bool
template_exists ( string template)
可以接受模板的文件系统路径,或者一个指定模板的资源字符串作为参数。
例子13-1. template_exists()
本例使用$_GET['page']来{include}一个内容模板。如果模板并不存在,那么改为显示一个错误页面。首先是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->template_exists($mid_template) ){ $mid_template = 'page_not_found.tpl'; } $smarty->assign('content_template', $mid_template);
$smarty->display('page_container.tpl');
?>
|
|
参见display(),fetch(),{include}以及{insert}。