Chapter 3. Template Language

Table of Contents
Basic Syntax
Variables
Variable Modifiers
Built-in Functions
Custom Functions

The templates are the language of Smarty. These are the files that the designers work with. They're basically pages made up of static content interspersed with template markup tags. These tags are placeholders for variables or blocks of logic.

Basic Syntax

All Smarty template tags are enclosed within delimiters. By default, these delimiters are { and }, but they can be changed.

For these examples, we will assume that you are using the default delimiters. In Smarty, all content outside of delimiters is displayed as static content, or unchanged. When Smarty encounters template tags, it attempts to interpret them, and displays the appropriate output in their place.

Comments

Template comments are surrounded by asterisks, and that is surrounded by the delimiter tags like so: {* this is a comment *} Smarty comments are not displayed in the final output of the template. They are used mainly for making the templates more understandable.

Example 3-1. Comments

{* Smarty *}

{* include the header file here *}
{include file="header.tpl"}

{include file=$includeFile}

{include file=#includeFile#}

{* display dropdown lists *}
<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>

Functions

Each Smarty tag either prints a variable or invokes some sort of function. Functions are processed and displayed by enclosing the function and its attributes into delimiters like so: {funcname attr1="val" attr2="val"}.

Example 3-2. function syntax

{config_load file="colors.conf"}

{include file="header.tpl"}

{if $name eq "Fred"}
	You are not allowed here
{else}
	Welcome, <font color="{#fontColor#}">{$name}!</font>
{/if}

{include file="footer.tpl"}

Both built-in functions and custom functions have the same syntax in the templates. Built-in functions are the inner workings of Smarty, such as if, section and strip. They cannot be modified. Custom functions are additional functions implemented via plugins. They can be modified to your liking, or you can add new ones. html_options and html_select_date are examples of custom functions.

Attributes

Most of the functions take attributes that specify or modify their behavior. Attributes to Smarty functions are much like HTML attributes. Static values don't have to be enclosed in quotes, but it is recommended for literal strings. Variables may also be used, and should not be in quotes.

Some attributes require boolean values (true or false). These can be specified as either unquoted true, on, and yes, or false, off, and no.

Example 3-3. function attribute syntax

{include file="header.tpl"}

{include file=$includeFile}

{include file=#includeFile#}

{html_select_date display_days=yes}

<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>