Search
Syndication
Sponsors

Archive for the ‘Smarty’ Category

Math in Smarty

Tuesday, February 24th, 2009

Math

Math can be applied directly to variable values.

Example 3-7. math examples

{$foo+1}

{$foo*$bar}

{* some more complicated examples *}

{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}

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

{$foo|truncate:”`$fooTruncCount/$barTruncFactor-1`”}

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

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

Embedding Vars in Double Quotes

Tuesday, February 24th, 2009

Embedding Vars in Double Quotes

Smarty will recognize assigned variables embedded in “double quotes” so long as the variable name contains only numbers, letters, under_scores and brackets[]. See naming for more detail.

With any other characters, for example a .period or $object>reference, then the variable must be surrounded by `backticks`.

You cannot embed modifiers, they must always be applied outside of quotes.

Example 3-5. Syntax examples

{func var=”test $foo test”} <– sees $foo
{func var=”test $foo_bar test”} <– sees $foo_bar
{func var=”test $foo[0] test”} <– sees $foo[0]
{func var=”test $foo[bar] test”} <– sees $foo[bar]
{func var=”test $foo.bar test”} <– sees $foo (not $foo.bar)
{func var=”test `$foo.bar` test”} <– sees $foo.bar
{func var=”test `$foo.bar` test”|escape} <– modifiers outside quotes!

Example 3-6. Practical examples

{* will replace $tpl_name with value *}
{include file=”subdir/$tpl_name.tpl”}

{* doesn’t replace $tpl_name *}
{include file=’subdir/$tpl_name.tpl’} <–

{* must have backticks as it contains a . *}
{cycle values=”one,two,`$smarty.config.myval`”}

{* same as $module['contact'].’.tpl’ in a php script
{include file=”`$module.contact`.tpl”}

{* same as $module[$view].’.tpl’ in a php script
{include file=”$module.$view.tpl”}

Attributes in smarty

Tuesday, February 24th, 2009

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-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=’smarty@example.com’}

Functions in Smarty

Tuesday, February 24th, 2009

Functions

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}!
{else}
hi, {$name}
{/if}

{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

Tuesday, February 24th, 2009

Variables

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
{$foo.bar} <– 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”
{$smarty.config.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}
{$foo.$bar.$baz}
{$foo[4].baz}
{$foo[4].$baz}
{$foo.bar.baz[4]}
{$foo->bar($baz,2,$bar)} <– passing parameters
{”foo”} <– static values are allowed

{* display the server variable “SERVER_NAME” ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}

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