PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

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




Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

smarty

Articles that share the tag smarty:

The Dynamic Duo of PEAR::DB and Smarty (42 tags)

Introducing Smarty: A PHP Template Engine (6 tags)

Three-Tier Development with PHP 5 (5 tags)

View All

oreilly

Articles that share the tag oreilly:

What Is Web 2.0 (2327 tags)

A Simpler Ajax Path (19 tags)

Ajax on Rails (17 tags)

Wireless Mesh Networking (15 tags)

Understanding MVC in PHP (12 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com