This site uses cookies in order for our service to work better. Learn more I understand

SMARTY template system

Windu CMS is using the SMARTY template system. Generally it is an engine that allows a more advanced and at the same time simpler way of using PHP from the HTML code level.

For example if we wanted to execute PHP code in our index.php file without SMARTY we'd have have to call out the code starting with php markers, with smarty it's simpler.
 
{$my_variable}
 
As you can see it's much simpler.

As you can probably see in Windu we're sometimes using single curly braces and sometimes double. Single braces are used in code of the CMS system and in widgets, however in templates we are using double braces system. What marker we're using can be declared in Smarty config. We've decided to use double braces so that user could use curly braces with no problems, since they are not a special marker calling out Smarty.
 

Syntax

Of course the variable has to be transfered to the Smarty engine. There are two ways to do it. We can call out the correct code from controller level in, meaning the same place where we define launch of the smarty engine (we will look into this in the part concerning widgets). The second option is defining a variable from template level.
 
{$foo}
{assign var=foo value=[1,[9,8],3]}   // can be nested

Short variable assignment:

{$foo=$bar+2}
{$foo = strlen($bar)}               // function in assignment
{$foo = myfunct( ($x+$y)*3 )}       // as function parameter 
{$foo.bar=1}                        // assign to specific array element
{$foo.bar.baz=1}                    
{$foo[]=1}                          // appending to an array

Smarty "dot" syntax (note: embedded {} are used to address ambiguities):

{$foo.a.b.c}        =>  $foo['a']['b']['c'] 
{$foo.a.$b.c}       =>  $foo['a'][$b]['c']         // with variable index
{$foo.a.{$b+4}.c}   =>  $foo['a'][$b+4]['c']       // with expression as index
{$foo.a.{$b.c}}     =>  $foo['a'][$b['c']]         // with nested index

PHP-like syntax, alternative to "dot" syntax:

{$foo[1]}             // normal access
{$foo['bar']}
{$foo['bar'][1]}
{$foo[$x+$x]}         // index may contain any expression
{$foo[$bar[1]]}       // nested index
{$foo[section_name]}  // smarty {section} access, not array access!

Variable variables:

$foo                     // normal variable
$foo_{$bar}              // variable name containing other variable 
$foo_{$x+$y}             // variable name containing expressions 
$foo_{$bar}_buh_{$blar}  // variable name with multiple segments
{$foo_{$x}}              // will output the variable $foo_1 if $x has a value of 1.

Object chaining:

{$object->method1($x)->method2($y)}

Direct PHP function access:

{time()}

Defining variables from PHP level
This is a general example, in case of Windu CMS variables will be defined only from widget controller level.
 
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');
$smarty->display('index.tpl');
Calling out variables defined in PHP
 
Hello {$firstname} {$lastname}, glad to see you can make it.

{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.


Modificators

Each variable can be easily modified, cut or can have other operations performed on it. In order to do so we have a whole range of very useful and simple tools.
Smarty documentation shows what they can be used for very well.
http://www.smarty.net/docs/en/language.modifiers.tpl
List of available modificators
  • capitalize - {$articleTitle|capitalize} - capitalizes the 1st letter in every word
  • cat - {$articleTitle|cat:' yesterday.'} - pastes in a variable at the end of a string
  • count_characters - {$articleTitle|count_characters} - returns the number of letters in the string
  • count_paragraphs - {$articleTitle|count_paragraphs } - returns the number of paragrafs in the string
  • count_sentences - {$articleTitle|count_sentences } - returns the number of sentences
  • count_words - {$articleTitle|count_words } - returns the number of words
  • date_format - {$yesterday|date_format:"%A, %B %e, %Y"}  - returns the formated date
  • default - {$articleTitle|default:'no title'} - in case of an empty string returns default defined value 
  • escape - {$articleTitle|escape:'html'} - filters out chosen types of signs, those are HTML tags in the example
  • from_charset - changes the type of string coding
  • indent - {$articleTitle|indent:10}  - adds a tab indentation for the string
  • lower - {$articleTitle|lower} - changes letters to lowercase
  • nl2br - {$articleTitle|nl2br} - changes mark of a new line for HTML BR marker
  • regex_replace - {$articleTitle|regex_replace:"/[rtn]/":" "} - allows use of regular phrases
  • replace - {$articleTitle|replace:'Garden':'Vineyard'} - changes chosen word for another one defined in tag
  • spacify - {$articleTitle|spacify:"^^"} - allows the implementation of a mark in between every letter for example a space or an apostrophe
  • string_format - {$number|string_format:"%.2f"} - allows fromating the string as a number
  • strip - {$articleTitle|strip:' '} - deletes or changes spacing at the begining and end of the row.
  • strip_tags - {$articleTitle|strip_tags} - deletes all tags and leaves "clean" text
  • to_charset - changes coding of the string
  • truncate - {$articleTitle|truncate:30} - cuts strings to a certain length
  • unescape {$articleTitle|unescape:"html"} - works in an opposite way to the tag escape
  • upper - changes letters into uppercase
  • wordwrap - {$articleTitle|wordwrap:20} - brakes up words longer than specified
 
Examples of use:
 
{* apply modifier to a variable *}
{$title|upper}

{* modifier with parameters *}
{$title|truncate:40:"..."}

{* apply modifier to a function parameter *}
{html_table loop=$myvar|upper}

{* with parameters *}
{html_table loop=$myvar|truncate:40:"..."}

{* apply modifier to literal string *}
{"foobar"|upper}

{* using date_format to format the current date *}
{$smarty.now|date_format:"%Y/%m/%d"}

{* apply modifier to a custom function *}
{mailto|upper address="smarty@example.com"}

{* using  php's str_repeat *}
{"="|str_repeat:80}

{* php's count *}
{$myArray|@count}

Modificators can be glued together and create different combinations. Examples of use:
 
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}
 

Atributes

In smarty we can transfer atributes, by either including the template file or calling out the function parameterizing it. It's used, for example, in the case of widgets or codes for language phrases.
 
{include file="header.tpl"}
{include file="header.tpl" nocache}  // is equivalent to nocache=true
{include file="header.tpl" attrib_name="attrib value"}
{include file=$includeFile}
{include file=#includeFile# title="My Title"}

{assign var=foo value={counter}}  // plugin result
{assign var=foo value=substr($bar,2,5)}  // PHP function result
{assign var=foo value=$bar|strlen}  // using modifier
{assign var=foo value=$buh+$bar|strlen}  // more complex expression

{html_select_date display_days=true}
{mailto address="smarty@example.com"}

More about smarty can be found in the documentation at: http://www.smarty.net/docs/en/ from where we took our examples.

Help Center

Do you have a technical problem?