Smarty – Passing ‘title’ variable to header template
January 23, 2011 – 6:46 pmGenerally when majority of your templates use the same headers and footers, it is obviously a good choice to split those out into their own templates and include them. But what if the header needs to have a different title, depending on what page you are coming from? For this you can pass the title to the header when it is included.
You can use title variable in header template like this:
homepage.tpl
{include file="header.tpl" title="Home Page"}
{* Rest of the template body will go here *}
aboutus.tpl
{config_load file="aboutus.conf"}
{include file="header.tpl" title=#aboutusTitle#}
{* Rest of the template body will go here *}
header.tpl
<HTML>
<HEAD>
<TITLE>{$title|default:"THIS IS DEFAULT TITLE"}</TITLE>
</HEAD>
<BODY>
When the Home page will be load, the title “Home Page” will be to the header.tpl, and will be used as the title. When the aboutuspage is drawn, the title will be “About us”. Which is used in archives_page.conf file, as we are using a variable from the archives_page.conf file instead of a hard coded variable. Also notice that “THIS IS DEFAULT TITLE” will be printed using the default variable modifier, if the $title variable is empty.