<HTML ><HEAD ><TITLE >Prefilters</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Advanced Features" HREF="advanced.features.html"><LINK REL="PREVIOUS" TITLE="Advanced Features" HREF="advanced.features.html"><LINK REL="NEXT" TITLE="Postfilters" HREF="advanced.features.postfilters.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="advanced.features.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 15. Advanced Features</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="advanced.features.postfilters.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="advanced.features.prefilters" ></A >Prefilters</H1 ><P > Template prefilters are PHP functions that your templates are ran through <SPAN CLASS="emphasis" ><I CLASS="emphasis" >before they are compiled</I ></SPAN >. This is good for preprocessing your templates to remove unwanted comments, keeping an eye on what people are putting in their templates, etc. </P ><P > Prefilters can be either <A HREF="api.register.prefilter.html" >registered</A > or loaded from the <A HREF="variable.plugins.dir.html" >plugins directory</A > by using <A HREF="api.load.filter.html" ><TT CLASS="varname" >load_filter()</TT ></A > function or by setting the <A HREF="variable.autoload.filters.html" > <TT CLASS="parameter" ><I >$autoload_filters</I ></TT ></A > variable. </P ><P > Smarty will pass the template source code as the first argument, and expect the function to return the resulting template source code. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6927" ></A ><P ><B >Example 15-2. Using a template prefilter</B ></P ><P > This will remove all the html comments in the template source. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// put this in your application<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">remove_dw_comments</span><span style="color: #007700">(</span><span style="color: #0000BB">$tpl_source</span><span style="color: #007700">, &</span><span style="color: #0000BB">$smarty</span><span style="color: #007700">)<br />{<br /> return </span><span style="color: #0000BB">preg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"/<!--#.*-->/U"</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #0000BB">$tpl_source</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// register the prefilter<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">register_prefilter</span><span style="color: #007700">(</span><span style="color: #DD0000">'remove_dw_comments'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >See also <A HREF="api.register.prefilter.html" ><TT CLASS="varname" >register_prefilter()</TT ></A >, <A HREF="advanced.features.postfilters.html" >postfilters</A > and <A HREF="api.load.filter.html" ><TT CLASS="varname" >load_filter()</TT ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="advanced.features.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="advanced.features.postfilters.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Advanced Features</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="advanced.features.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Postfilters</TD ></TR ></TABLE ></DIV ></BODY ></HTML >