Starting Templates

In real-world web development, templates can get much more complicated than this. Smarty templates are great for solving complicated development problems in a simple way. As we have discussed in previous chapters, if you properly plan before starting your template, it will save your time and money.

Before starting your template, make sure you have a solid understanding of the Smarty foundations. Don’t leap in after skim-reading this chapter! Paying careful and imaginative attention to the Smarty basics will prove worthwhile.

Templates in real life use arrays in a big way. Arrays in Smarty, like those in other programming languages are just a collection of variables. In PHP, arrays are of two different types, one is a non-associative array where array values have no key, and the other is associative where values are associated with keys. Let’s see these two types.

Non-associative Array

$students = array(“Didar”, “Emran”, “Hasan”, “Deborah”, “Shahana”);

In the above array example, $students is an array where $students[0] is Didar, $students[1] is Emran and so forth. It is the most common style of populating an array but associative arrays have some benefit over non-associative arrays.

Associative Array

$students = array(
       “didar” => array(“name”=>”Didar Bhuiyan”, “roll”=>12)
      “emran” => array(“name”=>”Emran Hasan”, “roll”=>18
      “hasan” => array(“name”=>”Tanveer Hasan”, “roll”=>23));

In this array, all items are pointed to by a key. For example $students[“didar”] is an array with two items, name and roll. So if you want to see what is the roll number of Didar then you can access it via $students[‘didar’][‘roll’]. This code is much more readable than something like $students[0][1]. You should try your best to use associative arrays in your templates for the sake of simplicity. In some complex cases, however, non-associative arrays are flexible to manage in Smarty templates.

Now that you have some basic idea about arrays, let’s see how to pass arrays to Smarty templates and how to manipulate them.

Passing Arrays to Smarty Templates and Manipulating Them

In this section, we will pass arrays from PHP code to the Smarty templating engine and discuss how to process them. You will find that it is very useful to pass an associative array instead of a non-associative one because you can access each element of this array with a readable name, which in turn, simplifies the code and also increases the extensibility. Let’s take a look at this code:

associative_array.php

<?
   include_once(“libs/smarty.class.php”);
   $smarty = new smarty();	
   $students = array(
      “didar” => array(“name”=>”Didar Bhuiyan”,”roll”=>12),

      “emran” => array(“name”=>”Emran Hasan”,”roll”=>18),
      “hasan” => array(“name”=>”Tanveer Hasan”,”roll”=>23));
   $smarty->assign(“students”,$students);
   $smarty->display(“associative_array.tpl”);  
?>

associative_array.tpl

<html>
<body>
<table>
<tr><td>Name</td><td>Roll</td></tr>
{foreach item=student from=$students}    
    <tr>
      <td>{$student.name}</td>
      <td>{$student.roll}</td>
</tr>
        {/foreach}
        </table>
        </body>
</html>

Run the associative_array.php file, and you will see output like this:

Passing Arrays to Smarty Templates and Manipulating Them

How is this done for every item in this array? If you look at the code, there is a {foreach} tag, which creates a loop. A loop is a block of code that repeats while a condition is true. Here this loop iterates through every element of the array. When it gets an element, note that it is also an array of a roll and a name item. So we can access them with a dot (.) sign after the array variable. {$student.roll} means the “roll” item of the current array, {$student.name} means the name.

If you pass the array as an object, you can access its elements using OOP-style PHP in your Smarty templates. Let’s see how to access them in OOP style.

oop_style.php

<?
include_once(“libs/smarty.class.php”);
class student
{
    var $name;
    var $roll;
}
$student = new student();
$student->name = “Arifin”;
$student->roll = “29”;

$smarty = new smarty();

$smarty->assign(“student”, $student);
$smarty->display(“oop_style.tpl”);
?>

oop_style.tpl

<table>
<tr><td>Name</td><td>Roll</td></tr>
    <tr>
      <td>{$student->name}</td>
      <td>{$student->roll}</td>
</tr>
</table>

The only important thing to point out here is the accessing style. Every item is accessed using OOP-style PHP, which means an object followed by an “->” operator.

Template designers, note down how the documentation by programmers can help you in these cases. This documentation helps you to create the loop, to access the array and of course to use the appropriate variable in the correct place. Don’t neglect the documentation. Remember that it will save you many sleepless nights.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.125.219