Smarty tutorial

What is Smarty? - Smarty is a PHP templating engine. Whenever you want the Web developer to create the SQL statements and mangle data, and Web designer to play with the visual outlay of the site with real data, and both not interfering with one another, a templating engine is a good idea.

How do I install Smarty? - Following the quick install guide will generally get you there. There are two things you need to do - install the PHP files belonging to smarty (hopefully under some directory that makes it reusable for your other Web projects, like /var/www/smarty, or /home/username/smarty), and create the templates, templates_c, cache and configs directories that Smarty will use.

How do I make sure Smarty runs? - Use the following code somewhere on your site inside a .php file. Down the line we’ll refer to this file as index.php:
// put full path to Smarty.class.php
require('/usr/local/lib/php/Smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = '/web/www.domain.com/smarty/templates';
$smarty->compile_dir = '/web/www.domain.com/smarty/templates_c';
$smarty->cache_dir = '/web/www.domain.com/smarty/cache';
$smarty->config_dir = '/web/www.domain.com/smarty/configs';
$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');

Make sure that the templates, templates_c and other directories have the correct paths. Those are site-specific, and should be changed as you use Smarty templates with different sites on your server.

Something’s wrong. It says it can’t display index.tpl - What you did in the code above is told Smarty to look for templates inside /web/www.domain.com/smarty/templates, create a new variable called name, assign it the value of Ned, and then display the template called index.tpl. The problem is that you don’t have the template file in place. Inside /web/www.domain.com/smarty/templates create a file called index.tpl, make sure it has the permissions readable by others (so something like 644 would work), and place the following HTML code in there:

<body>
Hello, {$name}!
</body>

Now load that index.php in your browser. If the config directories are in place, smarty is installed correctly, and the permissions are working, you should see a phrase Hello, Ned displayed in the browser. This is smarty engine displaying that $name variable set in index.php and displayed in index.tpl.

Cool! Can I have it capitalized? Yes, you can have it capitalized if you say
Hello, {$name|capitalize}! You could have it lowercased, if you say Hello, {$name|lower}! You could have it indented if you say Hello, {$name|indent}! You could have all the newlines replaced by the BR tag if you said Hello, {$name|nl2br}! You get the idea. The list of Smarty variable modifiers can be found here.

Can I have a Smarty template include other Smarty templates? - Yeah, it’s a pretty common deal for a Web page to include various components. Inside your .tpl file you can have something like this {include file="header.tpl"}

Can I pass variables to a template include in Smarty? - Yes, whenever you include a file, you can set your variables for that specific template {include file="header.tpl" title="Welcome to my site"}

How does caching work in Smarty? - Smarty caches the files that have been previously created. The logic goes the following way:
$smarty = new Smarty;
$smarty->caching = true;
if(!$smarty->is_cached('index.tpl'))
{
//assign some values here
}
$smarty->display('index.tpl');

How do I iterate over an array with Smarty? - Let’s say you’ve got an array of user comments, which is index-based, but then every element of that array is associative. The iteration can be done with a foreach loop:
{foreach item=comment from=$comments}
User: {$comment.author}
Comment title: {$comment.title}
Comment contents: {$comment.text}
{/foreach}

I am a designer, and have no clue what Smarty variables have been assigned to this template, can I see them? - Yes, add {debug} at any place inside the template and reload the page.

How do I add my own custom functions to Smarty? Waxjelly published a tutorial on writing custom smarty functions in PHP.

One Response to “Smarty tutorial”

  1. November 18th, 2006 at 3:24 pm #12 PHP optimization tips - █▬█ ₪

    [...] For templating, are you using Smarty? This is probably the fastest way to ensure the frequently viewed pages of the site are cached. 3 Replies [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>