Introduction To Templating in PHP with Smarty

Discussion in 'PHP' started by pradeep, Jun 6, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:
    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:
    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:
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice