With this function, you can create a custom function within your content. This can be useful when you need to perform the same action multiple times in your content.
Arguments
Argument | Description |
---|
name | The name of the template function |
[var ...] | Default variable value to pass local to the template function |
Examples
function for calculating an age based on a date
{{function name='age'}}{{strip}}
{{$parts = '-'|explode:$date}}
{{$yearDiff = "Y"|date - $parts[0]}}
{{$monthDiff = "m"|date - $parts[1]}}
{{$dayDiff = "d"|date - $parts[2]}}
{{if $monthDiff lt 0}}
{{$yearDiff = $yearDiff - 1}}
{{else}}
{{if $monthDiff eq 0 && $dayDiff lt 0}}
{{$yearDiff = $yearDiff - 1}}
{{/if}}
{{/if}}
{{$yearDiff}}
{{/strip}}{{/function}}
{{customer field='birthday' assign='birthday' default='1998-01-01'}}
{{age date=$birthday}} years
Output:
25 years
function with data enrichment
{{function name='list' level=0}}{{strip}}
<ul class="level{{$level}}">
{{foreach $data as $entry}}
{{if count($entry) > 1}}
<li>{{$entry@key}}</li>
{{list data=$entry level=$level+1}}
{{else}}
<li>{{$entry}}</li>
{{/if}}
{{/foreach}}
</ul>
{{/strip}}{{/function}}
{{* create an array to demonstrate *}}
{{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3'],'item4']}}
{{* run the array through the function *}}
{{list data=$menu}}
Output:
<ul class="level0">
<li>item1</li>
<li>item2</li>
<li>item3</li>
<ul class="level1">
<li>item3-1</li>
<li>item3-2</li>
<li>item3-3</li>
</ul>
</ul>
function to determine the current quarter
{{function name='currentQuarter'}}
{{$currentMonth = $smarty.now|date_format:'%m'}}
{{if $currentMonth lte 3}}
{{$return = 1}}
{{else if $currentMonth lte 6}}
{{$return = 2}}
{{else if $currentMonth lte 9}}
{{$return = 3}}
{{else}}
{{$return = 4}}
{{/if}}
{{$return}}
{{/function}}
The current quarter is: {{currentQuarter}}
Output based on the month July:
The current quarter is: 3