Smarty: Include header and footer template

In previous article, I had written about installing and using Smarty. In this article, I will be showing how you can include header and footer in Smarty.

index.php assigns contents and calls the template file index.tpl


<?php
require_once('classes/ConnectSmarty.class.php');

// create an object of the class included above
$smarty = new ConnectSmarty;

// assign content
$smarty->assign('name','Mukesh Chapagain');
$smarty->assign('address','Kathmandu, Nepal');

// display the content
$smarty->display('index.tpl');
?>

header.tpl contains the header part of html


<html>
<head>
<title> {$title|default:"no title"} </title>
</head>
<body>
<h2>Welcome to my site</h2>

footer.tpl contains the footer part of html


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

index.tpl contains the body of the html and it also calls header.tpl and footer.tpl. You can see that this template file also uses smarty date function to display current date.


{include file="header.tpl" title="Homepage"}

The author of this tutorial:<br/><br/>
Name: {$name} <br/>
Address: {$address} <br/>
Date: {$smarty.now|date_format:"%Y-%m-%d"} <br/>

{include file="footer.tpl"}

Hope it helps.
Thanks.