3.1.3.1 Smarty Logic


Smarty logic is used for (Example: to set a variable or check if a statement is true or false).

Below are some examples of Smarty Logic. It is possible to find more help on Smarty’s documentation.

 

Examples of IF statements

Logic is used on the programming side (Example: Show shopping cart contents).

*CODE* *SNIPPIT*{if $page.type == 'cart'}Indkøbskurv{/if}*CODE*

 

Examples of including sub templates

If you wish to include sub templates (Example: To include them on specific pages or columns you can use the following syntax):

*CODE* *SNIPPIT*{include file="sub-template.tpl"}*CODE*

Remember: That the template file should already be created in the system.

 

Examples of declaring variables

It is possible to declare your own variables (As Seen Below).

*CODE* *SNIPPIT*{$htmlVersion = 5}*CODE*

This gives the variable:

{$htmlVersion}

This is the output:

5

Variables can be declared as multidimensional arrays ($html.version = 5), and you can add this to arrays ($htmlVersion[] = 5). At the same time it is also possible to use mathematics on variables.

 

Examples of comparisons

It is possible to use different types of comparisons with the logic below:

  • eq / == - Equal to
  • neq / != - Not Equal to
  • gt / > - Greater than
  • lt / < - Less than
  • gte / >= - Greater than or Equal to
  • lte / <= - Less than or Equal to
  • or / || - Either this or that
  • and / && - Both this and that

 

Examples of IF / Else logic

It is possible to use 3 different IF statements (As Seen Below).

A simple IF statement:

*CODE* *SNIPPIT*{if $page.type == 'cart'}Shopping Cart{/if}*CODE*

IF/Else statement:

*CODE* *SNIPPIT*{if $page.type == 'cart'} Shopping Cart{else} No Shopping Cart{/if}*CODE*

IF/Elseif/Else statement:

*CODE* *SNIPPIT*{if $page.type == 'cart'} Shopping Cart {elseif == $page.type == 'checkout'} Order {else} Either Shopping Cart or Order. {/if}*CODE*

 

Examples of For Loops

A loop can be used when you want to output something many times (As Seen Below).

*CODE* *SNIPPIT*
    {for $i=1 to 3}
  • {$i}
  • {/for}
*CODE*

 

Examples of Foreach Loops

A Foreach loop can be used to loop through an array of data (As Seen Below).

Here are the arrays we’ll use:

*CODE* *SNIPPIT*{$htmlVersion[] = 4}{$htmlVersion[] = 5}*CODE*

The Foreach loop:

*CODE* *SNIPPIT*{foreach $htmlVersion as $version}{$version}{/foreach}*CODE*

The output is:

4 5