Tuesday, March 6, 2012

Use of PHP functions within Smarty code

Can standard PHP functions be used directly within Smarty code?  The answer is yes, but the proper syntax depends on where you use it!  For example, the syntax is different for 'assign' statements than for conditional statements.  See the examples below for getting a count from an array variable named 'aArray'.

Assignment syntax:

[{assign var="iCnt" value=$aArray|@count }]

Condition statement:

[{if count($aArray) == 0 }]DO WHATEVER[{/if}]

So in the case of assignments, a variable modifier must be used.  But within condition statements the function can be called directly.