Categories

Math in Smarty

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

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 [...]

Attributes in smarty

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). [...]

Functions in Smarty

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 [...]

Variables in Smarty

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 [...]

SMARTY_CORE_DIR constant

SMARTY_CORE_DIR

This is the full system path to the location of the Smarty core files. If not defined, Smarty will default this constant to the internals/ sub-directory below SMARTY_DIR. If defined, the path must end with a slash/. Use this constant when manually including any of the core.* files.

Example 11-2. SMARTY_CORE_DIR

// load core.get_microtime.php
require_once(SMARTY_CORE_DIR . ‘core.get_microtime.php’);

?>