February 6, 2014 2:38 pm

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

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

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:

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:

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

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

Smarty template to handle array:

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

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

  • Foreach Loop

PHP Code

Create an array with 3 items and assign to myMenu.

Template File

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:
  • Carlos u

    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

  • Wadson

    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.

  • Loganatan

    Good information. I used it.

  • pittendrigh

    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.

  • pittendrigh

    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.

  • pittendrigh

    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.

  • Thank you very much this topic very useful.

  • Thank you very much this topic very useful.