Template System Overview
Introduction
The SFTrade template system is very powerful and allows you to create nearly any type of design for the software generated pages. The template system is also used in the
e-mail messages that the software sends, so you can control the content of those messages as well. This document will give you an overview of the basic template features
you will need to understand.
Basic Syntax
With the SFTrade template system, all template tags are enclosed within the delimiters { and }. All content outside of delimiters is displayed as static content, or unchanged.
When the SFTrade template parser encounters template tags, it attempts to interpret them, and displays the appropriate output in their place.
Comments
Template comments are surrounded by asterisks, which are in turn surrounded by the delimiter tags like so:
{* this is a comment *}
Template comments are NOT displayed in the final output of the template, unlike <!-- HTML comments --> these are useful for making internal notes in the templates which
no one will see in the generated output. Unlike the majority of the template commands, template comments may span multiple lines, which means the beginning {* delimiter and ending *} delimiter
are not required to be on the same line in the template code.
Variables
Template variables start with the dollar sign ($) and can contain numbers, letters and underscores like so:
{$g_config}
{$trade.return_url}
{$user.username}
When a template variable is encountered by the template parser, the entire
template tag (including delimiters) is replaced by the value of that variable. Each of the templates will have different variables that you can use within them, so be sure to
review the other template sections of the manual to find out what variables are available in each template.
Variable Modifiers
In addition to simply displaying variables, you can also apply modifier functions to them. This is useful for formatting purposes, for example, when displaying a date you can
specify the format that the date should appear in. Variable modifiers follow the variable name and are separated from it by a | character like so:
{$g_stats.i_raw_60|t_tostring}
In the example above the SFTrade function t_tostring is applied to the {$g_stats.i_raw_60} variable before it is displayed in the template. You can use any of the built in PHP functions
as long as it takes the variable you specified as the first parameter.
It is also possible to specify additional function arguments with the modifiers. Function arguments will come after the variable modifier and will be enclosed in parenthesis. So, for example, if you wanted to use the PHP function number_format() you would do that like so:{$variable|number_format(2,'.',',')}
This would call the number_format PHP function with the arguments like this:
number_format($variable, 2, '.', ',')
SFTrade includes some builtin template modifiers, which are described below:
Functions
It is also possible to specify additional function arguments with the modifiers. Function arguments will come after the variable modifier and will be enclosed in parenthesis. So, for example, if you wanted to use the PHP function number_format() you would do that like so:
t_tostring
-
This will convert a numeric value into a friendly string (e.g. 18234 to 18,234)
t_chop
-
This will truncate a string to a specific length, and append a string to the end indicating that it has been truncated. This
modifier takes two arguments: length, string to append
t_chop(50,'...')
This example will truncate the string to 50 characters and append ... to the end of the truncated string
t_datetime
-
This will convert a datetime value into a string formatted as you like. This function takes a single argument: the
date/time format to use.
t_date
-
This will convert a date value into a string formatted as you like. This function takes a single argument: the
date format to use.
Function tags appear much like variable tags, but do not start with a dollar sign. These template tags will contain a function name followed by optional function attributes like so:
{function attribute1="value1" attribute2="value2"}
Some functions are block functions which have both an opening and closing tag. All of the HTML code that is inside the opening and closing tags will be processed by these functions.
Block functions look like this:
{function attribute1="value1" attribute2="value2"}
Some HTML code
or other template values
{/function}
The template system provides some stock functions which are available in all templates, and some custom functions which are only available in specific templates. The
Stock Functions are described in this section of the manual. Other functions that are available on specific pages will be covered in the appropriate sections of this manual.
Stock Functions
The SFTrade template system offers several stock functions that can be used in any of the software templates. These stock functions are described here.
{if}, {else}, {elseif}
In the {if} and {elseif} tags all PHP conditionals and functions are recognized, such as ||, or, &&, and, is_array(), etc. The conditionals and functions must be enclosed in the { and } delimiters and there must be a space after the "if" function name.
{foreach}, {foreachelse}, {foreachdone}
{foreachelse} There are no items to display {/foreach}
The {foreach} function has 3 available attributes:
-
from - This specifies the template variable to loop over
var - This specifies the variable name to use within the {foreach} block to display the values contained in the array
counter - This specifies the variable name that will be incremented for each item in the array
The from and var attributes are required; the counter attribute is optional.
If you would like to display some alternate HTML code when the array value is empty, you can use the {foreachelse} function. It must be contained with the {foreach} and {/foreach} tags. The HTML code that comes between the {foreachelse} and {/foreach} tags will be displayed when there are no values in the array specified in the from attribute.
You can break out of a {foreach} section early by using the {foreachdone} template function. When this is encountered, the {foreach} loop will immediately stop and will not continue processing the remainder of the items.
{insert}
-
counter - This specifies the name of the counter value that was specified in the {foreach} or {range} function
location - This specifies the location(s) where you want the HTML code displayed (more on this below)
max - This specifies the maximum counter value after which no further inserts will be done.
There are three ways you can specify a location: a single insertion, multiple insertions, or multiple insertions at regular intervals. To specify a single insertion, enter the location as a number by itself. To specify multiple insertions, enter a comma separated list of locations. To specify multiple insertions at regular intervals, enter the interval with a + sign in front of it. The location numbers you use are related to the counter value you are working with. Here are a few examples:
-
3,7,10 - This will insert the HTML code in the 3, 7, and 10 positions.
+5 - This will insert the HTML code every 5 positions.
25 - This will insert the HTML code only once at position 25
The max value can be used to stop the insertions after a specific position. For example, if you wanted to insert code every 5 positions but want it to stop after the 30th position you would set the location value to +5 and the max value to 30. This would still insert the HTML code at the 30th position, but not at the 35th, 40th, etc.
{capture}
{date}
-
value - This specifies the value to use to calculate the date that should be displayed/stored
format - This specifies the date and time format to use to display the date
The value setting allows you to offset the date so that you can display any date relative to today's current date. Some of the available offset values you can use are:
-
year - Offset by years
month - Offset by months
day - Offset by days
hour - Offset by hours
minute - Offset by minutes
second - Offset by seconds
Here are some examples of the settings you can use for the value attribute:
-
now - Today's date
today - Today's date
-1 day - Yesterday's date
-5 day - The date from 5 days ago
+2 day - The date 2 days from now
-1 week - The date 1 week ago
-1 week -2 day - The date 1 week and 2 days ago
2 days ago - The date 2 days ago
{locale}
{datelocale}
{literal}
{range}
-
start - This specifies the starting point of the loop. It can be an assigned template variable or a literal number
end - This specifies ending point of the loop. It can be an assigned template variable or a literal number
counter - This specifies the variable name that will be incremented for each loop iteration
{php}
{template}
In addition to the file attribute, you can also specify additional attributes which will then be accessible as template variables in the included template file. For example:
{include}
{cycle}
{assign}