PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Introducing Smarty: A PHP Template Engine
Pages: 1, 2, 3

Using Smarty

Now that you've installed Smarty on your server, let's get to the basics on using Smarty on your templates. Below is a simple PHP script that manipulates a template and assigns a template variable to a value:



<?php
require 'Smarty.class.php';
$smarty = new Smarty;

$smarty->assign("full_name","Tim O'Reilly");

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

Before doing anything, the script includes the Smarty class, which is included in Smarty.class.php. An object is created and the template variable (sometimes referred to as a template placeholder) named $full_name will hold the value "Tim O'Reilly."

Whenever you create your templates, the placeholders will be placed as {$variable_name}, but the placeholder delimiter can also be changed. This way you can use %$variable_name%, or anything you want.

In any case, the accompanying template file for this script is below:

<html>
<body>

O'Reilly & Associates Founder is {$full_name}

</body>
</html>

As explained above, {$full_name} will be substituted by the string "Tim O'Reilly" and outputted to the browser. Smarty also compiles the template into a PHP script, so the next time this script is requested, the template will not be parsed again (unless it changes) and the compiled PHP script will be used instead.

Control Flow with Smarty

Smarty also provides control-flow tags for developers:

{section name="i" loop=$list}
  <b>Iteration {$list[i]}</b><br>
{sectionelse}
  <b>Array $list has no entries</b>
{/section}

The {section} tag above provides a quick and simple way to loop through a list of values. All you need to do is to assign a variable to the $list variable and the {section} tag will loop through it. Like so:

<?php
require 'Smarty.class.php';
$smarty = new Smarty;

$list = array('Books', 'Are', 'Fun');
$smarty->assign("list", $list);

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

The output of this script will be:

<b>Books</b><br>
<b>Are</b><br>
<b>Fun</b><br>

The same thing is valid for associative arrays, but somewhat different:

<?php
require 'Smarty.class.php';
$smarty = new Smarty;

$list = array('name' => 'Tim',
        'last_name' => "O'Reilly"
);
$smarty->assign("list", $list);

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

The template source will be somewhat different for that associative array:

{if $list != ""}
  <b>Name: {$list.name}; Last Name: {$list.last_name}</b><br>
{else}
  <b>Array $list has no entries</b>
{/if}

Pages: 1, 2, 3

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: