Code Library: Templates Markup
HTML templates can contain any dynamic variables including Smart Tags, Page vars and language constants
[ - ] Close All
Smart Tags
Conditional Statements
{{
... }}
- conditional statement opening and closing tags
Conditional statements start with an opening {{
and end with a closing }}
and may also contain an if
condition.
You can put any kind of standard HTML, Javascript and CSS inside a conditional statement.
{{ My first name is {$FIRST_NAME} <br /> My last name is {$LAST_NAME} }}
Result:
My first name is Joe My last name is Bloggs
When there is no if
condition, all Smart Tags within {{ and }} must return true:
<p>Here is some info about me: <br />
{{
My first name is {$FIRST_NAME} <br />
My last name is {$LAST_NAME} <br />
My middle name is {$MIDDLE_NAME} <br />
}}
I don't have a middle name!</p>
Result:
Here is some info about me: I don't have a middle name!
if
- determine whether to display the statement with a condition
An if
condition must be after the opening {{
and end with a new line:
{{ if {$FIRST_NAME} My first name is {$FIRST_NAME} <br /> My last name is {$LAST_NAME} <br /> My middle name is {$MIDDLE_NAME} }}
Result:
My first name is Joe My last name is Bloggs My middle name is
Since we have qualified the statement by requiring only the first name, which returns true, the statement is output even though one of the other Smart Tags is empty.
Using an if
condition with a matching value:
Using the == operator in the condition means the variable must match the value exactly:
{{ if {$FIRST_NAME} == 'Joe' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
This would only be displayed to customers who are logged in and have a first name "Joe".
Using an if
condition that doesn't match the value:
{{ if {$FIRST_NAME} != 'Fuzzy Wuzzy' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
This also works to check if the Smart Tag is not empty:
{{ if {$FIRST_NAME} != '' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
else
- display an alternative when an if condition is false
You can use an else
after an if
:
{{ if {$COUNTRY_ID} == '40' <p> Your country id is 40 which means Tajikistan.</p> else <p> You do not live in is Tajikistan.</p> }}
Result:
You do not live in is Tajikistan.
The else
condition must appear on its own line:
{{ if {$FIRST_NAME} <p> Hello {$FIRST_NAME}.</p> else <p> Hello Sir .</p> // else should not have anything after it on the same line }} else // else must be inside the conditional statement
LIKE
- check for a partial match in the condition
Using an if
condition that has a partial match of the value:
{{ if {$FIRST_NAME} LIKE 'Jo' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
A LIKE
operator will return true for either a
to b
or b
to a
:
{{ if {$FIRST_NAME} LIKE 'Joseph Karen Ben Lisa Dan Joe Mike Mitch' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
NOT LIKE
- check for a no match in the condition
For no match we use the operator NOT LIKE
:
{{ if {$FIRST_NAME} NOT LIKE 'Joseph Karen Ben Lisa Dan Sean Mike Mitch' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
CONTAINS
- condition contains a value
For more specifically when only the variable contains the value we can use the operator CONTAINS
:
{{ if {$FIRST_NAME} CONTAINS 'Jo' <p> Welcome {$FIRST_NAME} </p> }}
Result:
Welcome Joe
IN
- condition is in a value
For when only the variable is in the value we use the operator IN
:
{{ if {$FIRST_NAME} IN 'Bill Joe Sally' <p> Welcome {$FIRST_NAME} </p> }}
|
- pipe separated values, (in array)
For an exact match for one of multiple values we use the pipe separator |
:
{{ if {$FIRST_NAME} IN 'Bill|Joe|Sally' <p> Welcome {$FIRST_NAME} </p> }}
Which means the following will return false:
{{ if {$FIRST_NAME} IN 'Bill|Joes|Sally' // Must match one of 'Bill' OR 'Joes' OR 'Sally' // Note: this is identical to // if {$FIRST_NAME} == 'Bill|Joes|Sally' <p> Welcome {$FIRST_NAME} </p> // 'Joe' is not an exact match for 'Joes' else <p> no exact match for '{$FIRST_NAME}' </p> }}
Result:
no exact match for 'Joe'
This is equivelent to the statement
if {$FIRST_NAME} == 'Bill' or {$FIRST_NAME} == 'Joes' or {$FIRST_NAME} == 'Sally'
// or similar to PHP in_array:
if (in_array({$FIRST_NAME}, array("Bill", "Joes", "Sally"))) {
$_PAGE
- parse a $_PAGE variable as a Smart Tag
You can use a $_PAGE
variable like a Smart Tag:
{{ if {$_PAGE['section']} == 'about' <p> You can also see our contact details on our <a href="/contact/">contact page</a>. </p> }}
$_SESSION
- parse a session variable as a Smart Tag
You can also use $_SESSION
variables in conditional statements, or anywhere in a HTML template:
{{ if {$_SESSION['country_id']} == '2' <p> Your country id is <b> {$_SESSION['country_id']} </b> which means United States.</p> }} {{ if {$_SESSION['browser']} !='IE' <p> Your browser is <b> {$_SESSION['browser']} </b> which is a good choice.</p> }}
Result:
Your country id is 2 which means United States. Your browser is Default Browser which is a good choice.
$_GET
- output a get value from a query string
You can also use $_GET
variables within statements:
{{ if {$_GET['vq']} CONTAINS 'phone' <p> You searched for <em>{$_GET['vq']}</em> - see also our <a href="/contact/">contact page</a>. }}
Result:
You searched for what is your phone number? - see also our contact page.page</a>.
__lang
- display a languange constant as a Smart tag
You can use __lang
constants in your HTML templates:
<b>{__pricing}</b>:<br /> {{ if {$WHOLESALER} {__wholesale_price}: $6.00 else {__price}: $12.95 }}
Result:
Pricing: Wholesale Price: $6.00
Comparison Operators
Operator | Meaning | Example |
---|---|---|
== |
is equal to or matches | {{ if{$COUNTRY_ID}=='2'
Yes we ship to you!
}} |
!= |
is not equal to, does not match | {{ if{$COUNTRY_ID}!='2'
Sorry we don't ship to you.
}} |
< |
is less than | {{ if{$VIEWS}<'2'
Welcome to our store!
}} |
> |
is greater than | {{ if{$ORDER_ITEMS}>'0'
You have {$ORDER_ITEMS} items in your cart.
}} |
<= |
is less than or equal to | {{ if{$ORDER_ITEMS_TOTAL}<='50'
Spend more than $50 for free shipping.
}} |
>= |
is greater than or equal to | {{ if{$ORDER_ITEMS_TOTAL}>='50'
You qualify for free shipping!
}} |
LIKE |
has a match: (e.g. 'ell' will match 'hello' or 'hello' will also match 'ell' ) | {{ if{$_GET['vq']} LIKE 'phone'
See our contact page for phone numbers
}} |
NOT LIKE |
has no match (or value is empty or NULL) | {{ if{$_PAGE['title']} NOT LIKE 'blog'
See our blog for updates.
// if title is empty will return true
}} |
CONTAINS |
variable contains the value: (e.g. 'hello' contains 'ell' but 'ell' does not contain 'hello') | {{ if{$_GET['vq']} CONTAINS 'phone'
Our phone is 010 101 101
}} |
IN |
variable is in the value: (e.g. 'ell' is in 'hello' but 'hello' is not in 'ell' ) | {{ if{$FIRST_NAME} IN 'Ben Joe Laura'
Hello Joe.
}} |
OR |
either variable in the condition are required to return true; can also use || |
{{ if{$FIRST_NAME} OR {$MIDDLE_NAME}
Hello Joe.
}} |
AND |
all variables in the condition are required to return true; can also use && |
{{ if{$FIRST_NAME} AND {$LAST_NAME}
Hello Joe Bloggs.
}} |
! |
is not: true if not exists; can also use =='' |
{{ if!{$FIRST_NAME}
Please log in.
}} |
| |
or: true if any one value | {{ if{$PAGE_SUBSECTION} == 'category|product'
If subsection is category OR product
}} |
Determining the booloean value of condition (without a comparison operator), e.g. if {$FIRST_NAME}
is similar to PHP:
$a=trim($a); if(!empty($a) && (!is_numeric($a) || (is_numeric($a) && $a!=0))) { // i.e. is true if it is not empty // after whitespace is trimmed // and is not equal to zero. // The inverse would be // if(empty($a) || (is_numeric($a) && $a==0)) }
Functions
There are a few basic functions similar to PHP functions:
Function | Meaning | Examples |
---|---|---|
include |
includes a HTML file; can be inside or outside a condtional statement, i.e. anywhere in a template. By default include from the same directory as the current template, but can be also template_id/filename.html
Must be a HTML file with a name ending with ".html" |
{ include filename.html }
{ include 4/filename.html }
{{ if{ ..condition.. }
{ include filename.html }
}} |
redirect |
redirects to a URL; Must be inside a condtional statement. Will not redirect to self, limit of 1 redirects per 30 seconds to prevent recursive loop. | {{ if{ ..condition.. }
{ redirect / }
}}
{{ if{ ..condition.. }
{ redirect https://www.website.com }
}} |
date |
display a PHP date format such as d-m-Y | { date 'd-m-Y' }
// 01-11-2024
{{ if { date 'm' } =='11'
it is nearly Christmas
}} |
Requirements
When a conditional statement has no if
condition it can be on one line:
{{ Hello {$FIRST_NAME} this is ok to be on one line since there is no if
condition. }}
Result:
Hello this is ok to be on one line since there is no if
condition.
The if
condition must come immediately after the opening {{
and have a closing }}
{{ <em>this is NOT ok since the if
condition must come first. </em>
if {$FIRST_NAME}
something something
}}
Result:
this is NOT ok since the if condition must come first. if something something
There can only be one if
condition inside a statement, the second occurence here is treated as HTML:
{{
if {$FIRST_NAME}
something
if {$LAST_NAME}
something
}}
Result:
something if Bloggs something
Instead, start a new statement:
{{ if {$FIRST_NAME} something }} {{ if {$LAST_NAME} something }}
Result:
something something
You can place a conditional statement anywhere within in your HTML templates:
<em class="{{ if {$FIRST_NAME} logged-in else logged-out }}">something something</em>
HTML output:
<em class="logged-in">something something</em>
Result:
something something
Comments can appear anywhere in a conditional statement, and can be either a new line with //
or within /*
and */
<p>{{ // this is a single line comment and will not be shown // its ok to have comments before your if condition if {$FIRST_NAME} Hello {$FIRST_NAME} /* this is another way of inserting hidden comments across multiple lines. */ something something <!-- an HTML comment is still rendered in the HTML source --> // extra white space and line returns inside a conditional statement is removed }}</p>
HTML output:
<p>Hello something something <!-- an HTML comment is still shown in the source --></p>
Result:
Hello something something
You can have multiple variables within if
conditions:
{{ if {$BROWSER} {$BROWSER_VERSION} == 'Default Browser ' <p> Your browser is {$BROWSER} {$BROWSER_VERSION} </p>. }}
Result:
Your browser is Default Browser
if
conditions and values don't need to be enclosed by single quotes:
{{ if {$FIRST_NAME} {$LAST_NAME} == Joe Bloggs Hello {$FIRST_NAME} {$LAST_NAME} something something }}
Result:
Hello Joe Bloggs something something
You may alternatively use the PHP style tags <?
and ?>
instead of {{
and }}
to open and close a conditional statement:
<? <p> <em>Hello {$FIRST_NAME}</em> </p> ?>
Result:
Hello
Warning: this does not equate to a PHP parser, and all content between <?
and ?>
is still treated as HTML.