Sample Chapter : smarty php template programming and applications

smarty php template programming and applications

smarty php template programming and applications

We bring you another sample chapter this time from the book smarty php template programming and applications

Chapter 6 delves into the programming side of Smarty. There are many powerful functions that
designers can use to make Smarty development easier and more productive. This chapter
introduces the most useful of these, and provides practical examples of functions in action.
With all the added power in the designer’s hands, there are bound to be mistakes occasionally.

  smartyfunctions (344.4 KiB, 141 hits)
You need to be a registered user to download this file.

You can buy the book in Ebook or hard copy here


Math in Smarty


Math can be applied directly to variable values.

Example 3-7. math examples



{* some more complicated examples *}


{if ($foo+$bar.test%$baz*134232+10+$b+10)}


{assign var=”foo” value=”`$foo+$bar`”}

See also the {math} function for complex equations and {eval}.


Attributes in smarty


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-4. function attribute syntax

{include file=’header.tpl’}

{include file=’header.tpl’ attrib_name=’attrib value’}

{include file=$includeFile}

{include file=#includeFile# title=’Smarty is cool’}

{html_select_date display_days=yes}

{mailto address=’’}


Functions in Smarty


Every Smarty tag either prints a variable or invokes some sort of function. These are processed and displayed by enclosing the function and its attributes within delimiters like so: {funcname attr1=’val1′ attr2=’val2′}.

Example 3-3. function syntax

{config_load file=’colors.conf’}

{include file=’header.tpl’}
{insert file=’banner_ads.tpl’ title=’Smarty is cool’}

{if $logged_in}
Welcome, {$name}!
hi, {$name}

{include file=’footer.tpl’ ad=$random_id}

Both built-in functions and custom functions have the same syntax within templates.
Built-in functions are the inner workings of Smarty, such as {if}, {section} and {strip}. There should be no need to change or modify them.
Custom functions are additional functions implemented via plugins. They can be modified to your liking, or you can create new ones. {html_options} and {popup} are examples of custom functions.


Variables in Smarty


Template variables start with the $dollar sign. They can contain numbers, letters and underscores, much like a PHP variable. You can reference arrays by index numerically or non-numerically. Also reference object properties and methods.

Config file variables are an exception to the $dollar syntax and are instead referenced with surrounding #hashmarks#, or via the $smarty.config variable.

Example 3-2. Variables

{$foo} <– displaying a simple variable (non array/object)
{$foo[4]} <– display the 5th element of a zero-indexed array
{$} <– display the “bar” key value of an array, similar to PHP $foo[‘bar’]
{$foo.$bar} <– display variable key value of an array, similar to PHP $foo[$bar]
{$foo->bar} <– display the object property “bar”
{$foo->bar()} <– display the return value of object method “bar”
{#foo#} <– display the config file variable “foo”
{$} <– synonym for {#foo#}
{$foo[bar]} <– syntax only valid in a section loop, see {section}
{assign var=foo value=’baa’}{$foo} <– displays “baa”, see {assign}

Many other combinations are allowed

{$foo->bar($baz,2,$bar)} <– passing parameters
{“foo”} <– static values are allowed

{* display the server variable “SERVER_NAME” ($_SERVER[‘SERVER_NAME’])*}

Request variables such as $_GET, $_SESSION, etc are available via the reserved $smarty variable.


Smarty Manual : 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, unlike <!-- HTML comments -->. These are useful for making internal notes in the templates which no one will see 😉

Example 3-1. Comments within a template

{* I am a Smarty comment, I don't exist in the compiled output  *}

{* another single line smarty comment  *}
<!-- HTML comment that is sent to the browser -->

{* this multiline smarty
   comment is
   not sent to browser

Multi line comment block with credits block
  @ author:
  @ maintainer:
  @ para:           var that sets block style
  @ css:            the style output

{* The header file with the main logo and stuff  *}
{include file='header.tpl'}

{* Dev note:  the $includeFile var is assigned in foo.php script  *}
<!-- Displays main content block -->
{include file=$includeFile}

{* this <select> block is redundant *}
<select name="company">
  {html_options options=$vals selected=$selected_id}

<!-- Show header from affiliate is disabled -->
{* $affiliate|upper *}

{* you cannot nest comments *}
<select name="company">
  {* <option value="0">-- none -- </option> *}
  {html_options options=$vals selected=$selected_id}

{* cvs tag for a template, below the 36 SHOULD be an american currency
. however its converted in cvs.. *}
{* &#36;Id: Exp &#36; *}
{* $Id: *}