Home » Smarty

Templating in Smarty

14 July 2008 126 views Popularity: 1% Share/Bookmark

email

You can also include other smarty template files in one smarty template. This brings the use of template function available in Smarty.

You may want to make a constant header and footer for your entire website. For this purpose, you can use the ‘include’ tag. Include tags are used for including other templates in the current template. Any variables available in the current template are also available within the included template. The include tag must have the attribute “file”, which contains the template resource path.

You can also pass variables to included templates as attributes. Attribute variables override current template variables. In our case, the ‘title’ variable is passed to the included template ‘header.tpl’. It the ‘title’ variable is not passed in the included template ‘header.tpl’ then the default value of the title assigned in ‘header.tpl’ will be printed.

We have included header.tpl and footer.tpl in the index.tpl file. The header.tpl file contains title and banner text. The footer.tpl file contains copyright information. This information is constant over the entire website. So, it would be easy to modify the header and footer content if we include these files in every individual template.

index.tpl

{include file="header.tpl" title="Homepage"}</p>
The author of this tutorial:<br/><br/>
Name: {$name} <br/>
Address: {$address} <br/>
Date: {$smarty.now|date_format:"%Y-%m-%d"} <br/></p>
{include file="footer.tpl"}

header.tpl

<html><head>
<title> {$title|default:"no title"} </title>
</head>

<body>
<h2>Welcome to my site</h2>

footer.tpl

<br/>
Copyright © Mukesh Chapagain
</body></p>
</html>

Download source code

Note: I have not included Smarty library files in this zip file. You can download the Smarty library files from http://smarty.php.net/download.php or directly from http://chapagain.googlecode.com/files/smarty.zip

Related posts:

  1. Installing the Smarty Template Engine
  2. File Upload in PEAR and Smarty
  3. An Introduction to Smarty Template Engine
  4. Creating selection list, using foreach and section loop in Smarty
  5. Pagination in PEAR and Smarty
  6. Using database in PEAR and Smarty
  7. Magento: Showing Store Selector / Switcher in header and footer
  8. Magento: Show Currency Selector in header
  9. Very simple add, edit, delete, display in PHP
  10. jQuery: Grey out background and preview image as popup
  • http://www.ailments.com Ailments Database

    Your guidelines will make my work easier and less time consuming to modify the header and footer content .Thanks for posting this information.

  • http://www.hamroawaaz.com/ United Voices

    quite interesting… it is nice to see nepali website also opting to better technologies as well as adapting to newer technologies like smarty as well.