{"id":516,"date":"2024-03-09T05:03:31","date_gmt":"2024-03-09T05:03:31","guid":{"rendered":"http:\/\/www.phpgang.com\/?p=516---adfdaf5c-09c8-47a8-ac1e-9acb951d3da8"},"modified":"2024-03-09T05:03:31","modified_gmt":"2024-03-09T05:03:31","slug":"how-to-use-smarty-template-engine-step-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/www.phpgang.com\/how-to-use-smarty-template-engine-step-by-step-tutorial_516.html","title":{"rendered":"How to use Smarty template engine in PHP"},"content":{"rendered":"

If you are developing\u00a0a web application then you have to separate templates from your logic\u00a0code 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.<\/p>\n

\"How<\/p>\n

So what is smarty?<\/strong><\/p>\n

Its a framework that separate your website’s presentation layer.<\/p>\n

Why we use smarty?<\/strong><\/p>\n

Quick easy and painless development.<\/p>\n

You can get this framework from smarty.net official website click here to download<\/a>.<\/p>\n

How to install it?<\/strong><\/p>\n

This is a very simple and easy to install just like 1..2..3<\/p>\n

Go ti the download page and download zip folder:<\/p>\n

\"smarty-template-zip\"<\/p>\n

 <\/p>\n

Now unzip that file and you will get directory structure like below.<\/p>\n

\"smarty-template-directory<\/p>\n

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

How to create Smarty first page?<\/strong><\/p>\n

Here we are ready with our directory settings and now I am going to show you first app:<\/p>\n

index.php<\/strong><\/p>\n

<?php\r\nrequire 'libs\/Smarty.class.php';\r\n\r\n$smarty = new Smarty;\r\n\r\n$smarty->assign(\"page_title\", \"Hello PHPGang!!\");\r\n$smarty->assign(\"page_content\", \"This is PHPGang Smarty template demo content\");\r\n\r\n$smarty->display('index.tpl');\r\n?><\/pre>\n

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

index.tpl<\/strong><\/p>\n

<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n\r\n<title>{$page_title}<\/title>\r\n\r\n<\/head>\r\n<body>\r\n\r\n    {$page_content} \r\n\r\n<\/body>\r\n<\/html><\/pre>\n

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

Using this example our page looks like this:<\/strong>
\n\"smarty-template-my-first-application\"
\nYour First application is ready…. \ud83d\ude42<\/p>\n

Include files in Smarty template not in PHP:<\/strong><\/p>\n

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

Using this code we include 2 files in index.tpl 1. header.tpl 2. footer.tpl cool its very very simple to include files.<\/p>\n

Conditions in Smarty templates:<\/strong><\/p>\n

<body>\r\n    {if}\r\n        {$page_content}\r\n    {else}\r\n        Default Page Content\r\n    {\/if}\r\n<\/body><\/pre>\n

Here we are checking in template if some value passed from php file in\u00a0$page_content<\/em> then show that else go for default text.<\/p>\n

Arrays<\/strong><\/p>\n

PHP file<\/p>\n

<?php\r\nrequire 'libs\/Smarty.class.php';\r\n\r\n$smarty = new Smarty;\r\n\r\n$smarty->assign(\"page_title\", \"Hello PHPGang!!\");\r\n$smarty->assign('contact_info',array('u_id'=>1,\r\n                                'u_name'=>'PHPGang.com',\r\n                                'u_email'=>'email@site.com',\r\n                                'u_phone'=>'091221323455'\r\n));\r\n\r\n$smarty->display('index.tpl');\r\n?><\/pre>\n

This file send an array to template in contact_info variable name let see how smarty handle this array.<\/p>\n

Smarty template to handle array:<\/strong><\/p>\n

<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n\r\n<title>{$page_title}<\/title>\r\n\r\n<\/head>\r\n<body>\r\n   id : {$contact_info.u_id} <br>\r\n   name : {$contact_info.u_name} <br>    \r\n   email : {$contact_info.u_email} <br>    \r\n   phone : {$contact_info.u_phone} <br>    \r\n<\/body>\r\n<\/html><\/pre>\n

Wow that’ so easy really very easy and simple to use arrays in template.<\/p>\n

In last but not least loops:<\/strong><\/p>\n

How to use loops and view data<\/p>\n