Skip to content

templateExists()

检查指定的模板是否存在

描述

bool

templateExists

string

template

它可以接受文件系统上模板的路径,或者指定模板的资源字符串。

这个例子使用 $_GET['page']{include} 一个内容模板。如果模板不存在,那么将显示错误页面。首先是 page_container.tpl

    <html>
    <head><title>{$title}</title></head>
    <body>
    {include file='page_top.tpl'}

    {* include middle content page *}
    {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}