<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Passing variable title to header template</title> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Smarty Manual"> <link rel="up" href="tips.html" title="Chapter 18. Tips & Tricks"> <link rel="prev" href="tips.default.var.handling.html" title="Default Variable Handling"> <link rel="next" href="tips.dates.html" title="Dates"> </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">Passing variable title to header template</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="tips.default.var.handling.html">Prev</a> </td> <th width="60%" align="center">Chapter 18. Tips & Tricks</th> <td width="20%" align="right"> <a accesskey="n" href="tips.dates.html">Next</a> </td> </tr> </table> <hr> </div> <div class="sect1" title="Passing variable title to header template"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="tips.passing.vars"></a>Passing variable title to header template</h2></div></div></div> <p> When the majority of your templates use the same headers and footers, it is common to split those out into their own templates and <a class="link" href="language.function.include.html" title="{include}"> <code class="varname">{include}</code></a> them. But what if the header needs to have a different title, depending on what page you are coming from? You can pass the title to the header as an <a class="link" href="language.syntax.attributes.html" title="Attributes">attribute</a> when it is included. </p> <div class="example"> <a name="id3131263"></a><p class="title"><b>Example 18.3. Passing the title variable to the header template</b></p> <div class="example-contents"> <p> <code class="filename">mainpage.tpl</code> - When the main page is drawn, the title of <span class="quote">“<span class="quote">Main Page</span>”</span> is passed to the <code class="filename">header.tpl</code>, and will subsequently be used as the title. </p> <pre class="programlisting"> {include file='header.tpl' title='Main Page'} {* template body goes here *} {include file='footer.tpl'} </pre> <p> <code class="filename">archives.tpl</code> - When the archives page is drawn, the title will be <span class="quote">“<span class="quote">Archives</span>”</span>. Notice in the archive example, we are using a variable from the <code class="filename">archives_page.conf</code> file instead of a hard coded variable. </p> <pre class="programlisting"> {config_load file='archive_page.conf'} {include file='header.tpl' title=#archivePageTitle#} {* template body goes here *} {include file='footer.tpl'} </pre> <p> <code class="filename">header.tpl</code> - Notice that <span class="quote">“<span class="quote">Smarty News</span>”</span> is printed if the <code class="literal">$title</code> variable is not set, using the <a class="link" href="language.modifier.default.html" title="default"><code class="varname">default</code></a> variable modifier. </p> <pre class="programlisting"> <html> <head> <title>{$title|default:'Smarty News'}</title> </head> <body> </pre> <p> <code class="filename">footer.tpl</code> </p> <pre class="programlisting"> </body> </html> </pre> </div> </div> <br class="example-break"> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="tips.default.var.handling.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="tips.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="tips.dates.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Default Variable Handling </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> Dates</td> </tr> </table> </div> </body> </html>