Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Introduction To Templating in PHP with Smarty (http://www.go4expert.com/articles/introduction-templating-php-smarty-t28531/)

pradeep 6Jun2012 19:23

Introduction To Templating in PHP with Smarty
 
Smarty is a templating engine for PHP, it's one of the most popular templating engine used by PHP developers. Using a templating engine helps businesses separate presentation logic & application logic.

Templating, you can say is a way to separate the business logic from the presentation logic. Most of the world's we application developers use either embedded scripting languages like PHP, JSP, ASP etc. while other use print statements in the scripting languages to output their HTML to the world. Templating can be overlooked for small pages and websites, but as your business logic becomes complex and you starting getting more hits, maintenance and upgradation becomes tough.

A templating system allows the designer to write their own HTML with special commands/placeholders in it for the programmer to integrate the business logic into it, thereby making each others lives easier.

Why Use Templates?



PHP being a embedded scripting language enabled developers to embed PHP code inside HTML files, which was very easy at a certain level, but as projects/requirements grew managing changes in layout (presentation logic) became tough & tiresome, so templating engines came to the rescue, they allow the designer to work without worrying about how the developer will display the data etc. all the designer needs to do it to use a combination of standard HTML tags, template tags & variables.

So, one day if the need arises for the layout to be completely changed there would not be any need for the developer to change his application logic, inversely if the developer needs to change the way data is fetched & processed it would not require any intervention of the designer.

Features of Smarty



Smarty is very fast, and efficient because the PHP parser does all the parsing, and the overhead is greatly reduced as the template is compiled & cached on the first run and Smarty smartly recompiles a template only if there are any modifications in the template.

Logical constructs like if, elseif, etc. help writing basic logic in the template itself. Smarty allows you to create your own custom functions & variable modifiers easing out a lot of coding on the developers' end.

There are many more small & tiny useful features which we'll discuss in upcoming articles, here we'll just go through basic usage of Smarty.

Installing Smarty



Smarty require PHP 5.2 & above, for installation instructions & details please refer to the amazingly documented Smarty documentation here.

Basic Usage



Follow the basic demo below to understand the basic usage of Smarty, I have added commented wherever required. We'll get deeper into Smarty in another article, till then explore your imagination.

The template file:
Code: HTML

<html>
<head>
    <!-- here we are using a variable modifier i.e. capitalize & a smarty special variable now -->
    <title>Messages for {$name|capitalize} on {$smarty.now|date_format:'%Y-%m-%d'}</title>
</head>
<body>
<p><b>{$name|capitalize}</b>, has {$num_messages} new messages.</p>

<p>Latest message:<br/> {$last_message|nl2br}</p>
</body>
</html>


The PHP code using Smarty:
Code: PHP

<?php

require_once(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty();

// this will automatically capitalized in the template output
$smarty->assign('name','shabbir');

$smarty->assign('num_messages',10);

// the new line in the message will automatically will be replaced with BR tag in the output
$smarty->assign('last_message',"Hello bro,\nHow are you doing?");

// this will process & output the final HTML
$smarty->display('messages.tpl');

?>


Finally the output looks like this:
Code: HTML

<html>
<head>
    <!-- here we are using a variable modifier i.e. capitalize & a smarty special variable now -->
    <title>Messages for Shabbir on 2012-06-06</title>
</head>
<body>
<p><b>Shabbir</b>, has 10 new messages.</p>

<p>Latest message:<br/> Hello bro,<br/>How are you doing?</p>
</body>
</html>


References



http://www.smarty.net/docs/en/index.tpl
http://en.wikipedia.org/wiki/Smarty


All times are GMT +5.5. The time now is 21:17.