Smarty Templating Basics

  Previous article Next article  

Smarty Templating Basics

  Article optimized for CMSMS 2.x

  Author:
  Last updated: 15-11-2017
  Comments: 0
  http://cms.ms/Bo5e

CMS Made Simple™ uses Smarty to build core and third party module templates. You can do *so much* with it, but you need to have some basic knowledge to start really using it to the fullest. In this tutorial I will give you an oversight of the most useful codes, including practical examples.

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

This tutorial will always be "work in progress". I will add/change items when I see a related question at the forum or when I come across a problem/solution myself.

  How to use

 Assign a value to a variable

{$foo = 'value'}

Read more about Assign 

A variable can be a:

  • string - a piece of text, "lorem ipsum"
  • integer - a whole number, "34"
  • double/float - a fraction, "32,16"
  • boolean - true/false

 Assign a value to a global variable

{$foo = 'value' scope=global}

Read more about Assign 


 Assign multiple values to one variable

{capture assign=foo}{uploads_url}/images/path/{$image_name}.jpg{/capture}

{$foo} will be "http://www.website.com/uploads/images/path/photo-name.jpg"

Read more about Assign 


 Only display variable when it has a value

{if !empty($foo)} <-- Means, if $foo is not empty
  <div class="bar">
    Value: {$foo}
  </div>
{/if}

 Only display content block when it is filled

{$content2 = "{content block='content2'}"}

{if !empty($content2)} <-- Means, if $content2 is NOT empty
  <div id="content2">
    {$content2}
  </div>
{/if}

 Only display when variable is empty

{if empty($foo)} <-- Means, if $foo is empty
  <!-- $foo has no value -->
{/if}

Equation

 Equations

!   NOT
&&  AND
||  OR
==  EQUALS
!=  NOT EQUALS


Modifiers

 Always display in lower case

This is used to lowercase a variable.

{$foo|lower}

'AbCdEf' will be 'abcdef'
'abcdef' wil be 'abcdef'

Read more about Lower 


 Always display in upper case

This is used to uppercase a variable.

{$foo|upper}

'AbCdEf' will be 'ABCDEF'
'ABCDEF' wil be 'ABCDEF'

Read more about Upper 


 Capitalize words

This is used to capitalize the first letter of all words in a variable.

{$foo|capitalize}

'abc def ghi' will become 'Abc Def Ghi'

Read more about Capitalize 


 Give a variable a default value

{$foo|default:'Default value'}
{$image|default:'default-image.jpg'}
 

Read more about Default 


 Rounding value

{$foo|round}

'20,23' will become '20'


 Truncate value

{$foo|truncate:30}

Read more about Truncate 


 Get a part of a variable

Return the portion of variable specified by the start and length parameters.

{$foo|substr:10}

Read more about Substr 


 Price and format

Return the variable with two decimals and replace the dot for a comma.

{$price = '12345'}
{$price|number_format:2:',':'.'}

The output will be 12.345,00 instead of 12345




Calculating

 Count values

{$a = '60'}
{$b = '30'}
{$a + $b}

The output will be 90


 Substract values

{$a = '60'}
{$b = '30'}
{$a - $b}

The output will be 30


 Multiply values

{$a = '6'}
{$b = '20'}
{$a * $b}

The output will be 120


 Divide values

{$a = '120'}
{$b = '20'}
{$a / $b}

The output will be 6


 Calculate percentage

{$part = '20'}
{$total = '80'}
{math equation='part/total*100' part=$part total=$total assign='percentage'}
{$percentage|round}

Result will be 25%




Show related articles:





CMS Made Simple 2.2.5 - Tutorials, tips and tricks - CMSMS 2.2.5

Smarty Templating Basics

  Article optimized for CMSMS 2.x

  Author:
  Last updated: 15-11-2017
  Comments: 0
  http://cms.ms/Bo5e