<HTML ><HEAD ><TITLE >fetch()</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="Smarty Class Methods()" HREF="api.functions.html"><LINK REL="PREVIOUS" TITLE="display()" HREF="api.display.html"><LINK REL="NEXT" TITLE="get_config_vars()" HREF="api.get.config.vars.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" 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="api.display.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="api.get.config.vars.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="api.fetch" ></A >fetch()</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN5803" ></A >fetch() -- returns the template output</DIV ><DIV CLASS="refsect1" ><A NAME="AEN5806" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >fetch</B > ( string template [, string cache_id [, string $compile_id]])<BR ></BR ><P > This returns the template output instead of <A HREF="api.display.html" >displaying</A > it. Supply a valid <A HREF="template.resources.html" >template resource</A > type and path. As an optional second parameter, you can pass a <TT CLASS="parameter" ><I >$cache id</I ></TT >, see the <A HREF="caching.html" >caching section</A > for more information. </P ><P > As an optional third parameter, you can pass a <TT CLASS="parameter" ><I >$compile_id</I ></TT >. This is in the event that you want to compile different versions of the same template, such as having separate templates compiled for different languages. Another use for <TT CLASS="parameter" ><I >$compile_id</I ></TT > is when you use more than one <A HREF="api.variables.html#variable.template.dir" ><TT CLASS="parameter" ><I >$template_dir</I ></TT ></A > but only one <A HREF="variable.compile.dir.html" ><TT CLASS="parameter" ><I >$compile_dir</I ></TT ></A >. Set a separate <TT CLASS="parameter" ><I >$compile_id</I ></TT > for each <A HREF="api.variables.html#variable.template.dir" ><TT CLASS="parameter" ><I >$template_dir</I ></TT ></A >, otherwise templates of the same name will overwrite each other. You can also set the <A HREF="variable.compile.id.html" > <TT CLASS="parameter" ><I >$compile_id</I ></TT ></A > variable once instead of passing this to each call to this function. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5838" ></A ><P ><B >Example 13-1. fetch()</B ></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: #007700">include(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">caching </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// only do db calls if cache doesn't exist<br /></span><span style="color: #007700">if(!</span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">is_cached</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">)) {<br /><br /> </span><span style="color: #FF8000">// dummy up some data<br /> </span><span style="color: #0000BB">$address </span><span style="color: #007700">= </span><span style="color: #DD0000">'245 N 50th'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$db_data </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'City' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Lincoln'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'State' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Nebraska'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'Zip' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'68502'<br /> </span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'Name'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Fred'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'Address'</span><span style="color: #007700">,</span><span style="color: #0000BB">$address</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_data</span><span style="color: #007700">);<br /><br />}<br /><br /></span><span style="color: #FF8000">// capture the output<br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// do something with $output here<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$output</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5842" ></A ><P ><B >Example 13-2. Using fetch() to send an email</B ></P ><P > The <TT CLASS="filename" >email_body.tpl</TT > template </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Dear {$contact.name}, Welcome and thankyou for signing up as a member of our user group, Click on the link below to login with your user name of '{$contact.login_id}' so you can post in our forums. http://{$smarty.server.SERVER_NAME}/login/ List master Some user group {include file='email_disclaimer.tpl'}</PRE ></TD ></TR ></TABLE ><P > The <TT CLASS="filename" >email_disclaimer.tpl</TT > template which uses the <A HREF="language.function.textformat.html" > <TT CLASS="varname" >{textformat}</TT ></A > modifier. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{textformat wrap=40} Unless you are named "{$contact.name}", you may read only the "odd numbered words" (every other word beginning with the first) of the message above. If you have violated that, then you hereby owe the sender 10 GBP for each even numbered word you have read {/textformat}</PRE ></TD ></TR ></TABLE ><P > The php script using the PHP <A HREF="http://php.net/function.mail" TARGET="_top" > <TT CLASS="varname" >mail()</TT ></A > function </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// get contact from database eg using pear or adodb<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">'select name, email, login_id from contacts where contact_id='</span><span style="color: #007700">.</span><span style="color: #0000BB">$contact_id</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$contact </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">getRow</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">, </span><span style="color: #0000BB">$contact</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">mail</span><span style="color: #007700">(</span><span style="color: #0000BB">$contact</span><span style="color: #007700">[</span><span style="color: #DD0000">'email'</span><span style="color: #007700">], </span><span style="color: #DD0000">'Subject'</span><span style="color: #007700">, </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">'email_body.tpl'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="language.function.fetch.html" ><TT CLASS="varname" >{fetch}</TT ></A > <A HREF="api.display.html" ><TT CLASS="varname" >display()</TT ></A >, <A HREF="language.function.eval.html" ><TT CLASS="varname" >{eval}</TT ></A >, and <A HREF="api.template.exists.html" ><TT CLASS="varname" >template_exists()</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="api.display.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="api.get.config.vars.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >display()</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="api.functions.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >get_config_vars()</TD ></TR ></TABLE ></DIV ></BODY ></HTML >