March 9, 2024 5:03 am

How to use Smarty template engine in PHP

If you are developing a web application then you have to separate templates from your logic code and make your application simple. Using smarty you can design your web pages without the care of your application code. Smarty template engine work as a language you can add conditions, PHP Code, loops and many more.

How to use smarty template engine in PHP

So what is smarty?

Its a framework that separate your website’s presentation layer.

Why we use smarty?

Quick easy and painless development.

You can get this framework from smarty.net official website click here to download.

How to install it?

This is a very simple and easy to install just like 1..2..3

Go ti the download page and download zip folder:

smarty-template-zip

 

Now unzip that file and you will get directory structure like below.

smarty-template-directory structure

This will be directory for your smarty engine you have to give writing rights to cache and templates_c directories so files can be write in these directories. Save your app templates in *.tpl extensions and save in templates folder.

How to create Smarty first page?

Here we are ready with our directory settings and now I am going to show you first app:

index.php

<?php
require 'libs/Smarty.class.php';

$smarty = new Smarty;

$smarty->assign("page_title", "Hello PHPGang!!");
$smarty->assign("page_content", "This is PHPGang Smarty template demo content");

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

Above page we include smarty class and create its object after that we assign data to 2 variables $smarty->assign(“page_title”, “Hello PHPGang!!”); and after that used display function to push that data to template and generate view.

index.tpl

<!DOCTYPE html>
<html lang="en">
<head>

<title>{$page_title}</title>

</head>
<body>

    {$page_content} 

</body>
</html>

This is the template file we used title and content in side this template getting data from index.php file we have to write variables in curly brackets with $ sign.

Using this example our page looks like this:
smarty-template-my-first-application
Your First application is ready…. 🙂

Include files in Smarty template not in PHP:

{include file="header.tpl"}
<div>
    {$page_content} 
</div>
{include file="footer.tpl"}

Using this code we include 2 files in index.tpl 1. header.tpl 2. footer.tpl cool its very very simple to include files.

Conditions in Smarty templates:

<body>
    {if}
        {$page_content}
    {else}
        Default Page Content
    {/if}
</body>

Here we are checking in template if some value passed from php file in $page_content then show that else go for default text.

Arrays

PHP file

<?php
require 'libs/Smarty.class.php';

$smarty = new Smarty;

$smarty->assign("page_title", "Hello PHPGang!!");
$smarty->assign('contact_info',array('u_id'=>1,
                                'u_name'=>'PHPGang.com',
                                'u_email'=>'[email protected]',
                                'u_phone'=>'091221323455'
));

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

This file send an array to template in contact_info variable name let see how smarty handle this array.

Smarty template to handle array:

<!DOCTYPE html>
<html lang="en">
<head>

<title>{$page_title}</title>

</head>
<body>
   id : {$contact_info.u_id} <br>
   name : {$contact_info.u_name} <br>    
   email : {$contact_info.u_email} <br>    
   phone : {$contact_info.u_phone} <br>    
</body>
</html>

Wow that’ so easy really very easy and simple to use arrays in template.

In last but not least loops:

How to use loops and view data

  • For Loop
<ul>
{for $i=1 to 3}
    <li>{$i}</li>
{/for}
</ul>

// Out put of this loop

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

$i = 1 is starting point of the loop and run until I <=3 and generate list item on each round.

  • Foreach Loop

PHP Code

<?php
$menu = array('Home', 'Login', 'Signup');
$smarty->assign('myMenu', $menu);
?>

Create an array with 3 items and assign to myMenu.

Template File

<ul>
{foreach from=$myMenu item=menu}
    <li>{$menu}</li>
{/foreach}
</ul>

// Output looks like this

<ul>
    <li>Home</li>
    <li>Login</li>
    <li>Signup</li>
</ul>

Foreach loop get items from $myMenu and assign to menu.

There is many more things in Smarty this is just basic startup tips I hope you like this and please feel free to comment below.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

9 responses to “How to use Smarty template engine in PHP”

  1. Carlos u says:

    Hi, thaks for the info im a php programmer and didn’t know Smarty, looks similar to Twig. Im using twig right now and separate view layer with this tools really makes a clean code. ill try Smarty too .. thanks

  2. Wadson says:

    That’s just amazing. I was looking for some info like this. You just make me understand it better. Smarty is really simple and easy to learn yet a powerful template engine. Thanks, it was really hepful.

  3. Loganatan says:

    Good information. I used it.

  4. pittendrigh says:

    I have a legitimate question. I am trying my best not to sound like a troll. What is the point of Smarty Template? When I was a Java developer a template system made good sense. Templates in Java allow HTML developers to change content with a text editor, without re-compiling any Java codes. In PHP the templating system doesn’t seem to “separate logic from content” to me. Instead the template codes layer on a new level of obfuscation and complexity. file_get_contents($path) can import raw HTML, styled by css, that is less daunting to semi-skilled content editors than much the same thing brought in by SmartyTemplate. But the SmartyTemplate stuff is wrapped and hidden inside obscure programming codes that tend to make semi-skilled eyes glaze over.

  5. pittendrigh says:

    I have a legitimate question. I am trying my best not to sound like a troll. What is the point of Smarty Template? When I was a Java developer a template system made good sense. Templates in Java allow HTML developers to change content with a text editor, without re-compiling any Java codes. In PHP the templating system doesn’t seem to “separate logic from content” to me. Instead the template codes layer on a new level of obfuscation and complexity. file_get_contents($path) can import raw HTML, styled by css, that is less daunting to semi-skilled content editors than much the same thing brought in by SmartyTemplate. But the SmartyTemplate stuff is wrapped and hidden inside obscure programming codes that tend to make semi-skilled eyes glaze over.

  6. pittendrigh says:

    I have a legitimate question. I am trying my best not to sound like a troll. What is the point of Smarty Template? When I was a Java developer a template system made good sense. Templates in Java allow HTML developers to change content with a text editor, without re-compiling any Java codes. In PHP the templating system doesn’t seem to “separate logic from content” to me. Instead the template codes layer on a new level of obfuscation and complexity. file_get_contents($path) can import raw HTML, styled by css, that is less daunting to semi-skilled content editors than much the same thing brought in by SmartyTemplate. But the SmartyTemplate stuff is wrapped and hidden inside obscure programming codes that tend to make semi-skilled eyes glaze over.

  7. Thank you very much this topic very useful.

  8. Thank you very much this topic very useful.

  9. adil says:

    hi
    i want to call header.php in smaty templates file how can i do this please help

Leave a Reply

Your email address will not be published. Required fields are marked *