GDPR privacy disclaimer
This form will send me your email address and your IP address.
If you don't want that, don't use this form!
Smarty Templating Basics
Previous article Next articleCMS 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
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
Assign multiple values to one variable
{$foo} will be "http://www.website.com/uploads/images/path/photo-name.jpg"
Only display variable when it has a value
<div class="bar">
Value: {$foo}
</div>
{/if}
Only display content block when it is filled
{if !empty($content2)} <-- Means, if $content2 is NOT empty
<div id="content2">
{$content2}
</div>
{/if}
Only display when variable is empty
<!-- $foo has no value -->
{/if}
Equation
Equations
&& AND
|| OR
== EQUALS
!= NOT EQUALS
Modifiers
Always display in lower case
This is used to lowercase a variable.
'AbCdEf' will be 'abcdef'
'abcdef' wil be 'abcdef'
Always display in upper case
This is used to uppercase a variable.
'AbCdEf' will be 'ABCDEF'
'ABCDEF' wil be 'ABCDEF'
Capitalize words
This is used to capitalize the first letter of all words in a variable.
'abc def ghi' will become 'Abc Def Ghi'
Give a variable a default value
{$image|default:'default-image.jpg'}
Rounding value
'20,23' will become '20'
Truncate value
Get a part of a variable
Return the portion of variable specified by the start and length parameters.
Price and format
Return the variable with two decimals and replace the dot for a comma.
{$price|number_format:2:',':'.'}
The output will be 12.345,00 instead of 12345
Calculating
Count values
{$b = '30'}
{$a + $b}
The output will be 90
Substract values
{$b = '30'}
{$a - $b}
The output will be 30
Multiply values
{$b = '20'}
{$a * $b}
The output will be 120
Divide values
{$b = '20'}
{$a / $b}
The output will be 6
Calculate percentage
{$total = '80'}
{math equation='part/total*100' part=$part total=$total assign='percentage'}
{$percentage|round}
Result will be 25%
Comment Form
ReviewManager
ReviewManager
3 Comments
It stripped out my code so I guess there's nothing we can do here.
I'm taking a shot that maybe you can help me with this. I'm doing a site that has a mobile component. I'm using this UDT to detect mobiles:
Simple but works very well. In my template I put {mobiledetect} and bingo it changes to the mobile site. Problem is I don't want to use a peticular template on one of my menu items.
So in the template I put {if ! mobiledetect} blah blah blah{/if}
It always returns true. Whether it be a PC or a mobile. I'm close but not quite there.
Thanks in advance and I'd be glad to buy you a cup of Joe!
Doug
Thank you for the introducton!
Is there any chance to upgrade the current smarty version with the latest 3.x version in cmsms 2.1.5?