<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>template_exists()</title> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Smarty Manual"> <link rel="up" href="api.functions.html" title="Chapter 13. Smarty Class Methods()"> <link rel="prev" href="api.trigger.error.html" title="trigger_error()"> <link rel="next" href="api.unregister.block.html" title="unregister_block()"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">template_exists()</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="api.trigger.error.html">Prev</a> </td> <th width="60%" align="center">Chapter 13. Smarty Class Methods()</th> <td width="20%" align="right"> <a accesskey="n" href="api.unregister.block.html">Next</a> </td> </tr> </table> <hr> </div> <div class="refentry" title="template_exists()"> <a name="api.template.exists"></a><div class="titlepage"></div> <div class="refnamediv"> <h2>Name</h2> <p>template_exists() — checks whether the specified template exists</p> </div> <div class="refsect1" title="Description"> <a name="id3107228"></a><h2>Description</h2> <code class="methodsynopsis"><span class="type">bool </span><span class="methodname">template_exists</span>(<span class="methodparam"><span class="type">string </span><span class="parameter">template</span></span>);</code><p> It can accept either a path to the template on the filesystem or a resource string specifying the template. </p> <div class="example"> <a name="id3107270"></a><p class="title"><b>Example 13.26. template_exists()</b></p> <div class="example-contents"> <p> This example uses <code class="literal">$_GET['page']</code> to <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a> a content template. If the template does not exist then an error page is displayed instead. First the <code class="filename">page_container.tpl</code> </p> <pre class="programlisting"> <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> </pre> <p> And the php script </p> <pre class="programlisting"> <?php // set the filename eg 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'); ?> </pre> </div> </div> <br class="example-break"><p> See also <a class="link" href="api.display.html" title="display()"><code class="varname">display()</code></a>, <a class="link" href="api.fetch.html" title="fetch()"><code class="varname">fetch()</code></a>, <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a> and <a class="link" href="language.function.insert.html" title="{insert}"><code class="varname">{insert}</code></a> </p> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="api.trigger.error.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="api.functions.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="api.unregister.block.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">trigger_error() </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> unregister_block()</td> </tr> </table> </div> </body> </html>