
The Campsite template engine was built upon Smarty so the Campsite template language is actually an extension of the Smarty template language. For details on the Smarty template language please go to this link: http://smarty.net/manual/en/smarty.for.designers.php
All template tags are enclosed within delimiters. By default these are { and }, but they can be changed. In Campsite we use {{ and }}.
For the examples in this manual, we will assume that you are using the default Campsite delimiters. All content outside of delimiters is displayed as static content, or unchanged. When the template engine encounters template tags, it attempts to interpret them, and displays the appropriate output in their place.
The following sub-chapters, which were copied from the Smarty manual, will familiarize you with the Smarty syntax.
Template comments are surrounded by asterisks, and that is surrounded by the delimiter tags like so:
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 ;-)
Example 3-1. Comments within a template
{{* I am a template comment, I don't exist in the compiled output *}}
<html>
<head>
<title>{{$title}}</title>
</head>
<body>
{{* another single line comment *}}
<!-- HTML comment that is sent to the browser -->
{{* this multiline
comment is
not sent to browser
*}}
{{*********************************************************
Multi line comment block with credits block
@ author: bg@example.com
@ maintainer: support@example.com
@ para: var that sets block style
@ css: the style output
**********************************************************}}
{{* The header file with the main logo and stuff *}}
{{include file='header.tpl'}}
{{* Dev note: the $includeFile var is assigned in foo.php script *}}
<!-- Displays main content block -->
{{include file=$includeFile}}
{{* this <select> block is redundant *}}
{{*
<select name="company">
{{html_options options=$vals selected=$selected_id}}
</select>
*}}
<!-- Show header from affiliate is disabled -->
{{* $affiliate|upper *}}
{{* you cannot nest comments *}}
{{*
<select name="company">
{{* <option value="0">-- none -- </option> *}}
{{html_options options=$vals selected=$selected_id}}
</select>
*}}
{{* cvs tag for a template, below the 36 SHOULD be an american currency
. however its converted in cvs.. *}}
{{* $Id: Exp $ *}}
{{* $Id: *}}
</body>
</html>
Template variables start with the $dollar sign. They can contain numbers, letters and underscores, much like a PHP variable. You can reference arrays by index numerically or non-numerically. Also reference object properties and methods.
Config file variables are an exception to the $dollar syntax and are instead referenced with surrounding #hashmarks#, or via the $smarty.config variable.
Request variables such as $_GET, $_SESSION, etc are available via the reserved $smarty variable.
See also $smarty, config variables {{assign}} and assign().
Every Smarty tag either prints a variable or invokes some sort of function. These are processed and displayed by enclosing the function and its attributes within delimiters like so: {{funcname attr1='val1' attr2='val2'}}.
Both built-in functions and custom functions have the same syntax within templates.
Built-in functions are the inner workings of Smarty, such as {{if}}, {{section}} and {{strip}}. There should be no need to change or modify them.
Custom functions are additional functions implemented via plugins. They can be modified to your liking, or you can create new ones. {{html_options}} and {{popup}} are examples of custom functions.
See also register_function()
Most of the functions take attributes that specify or modify their behavior. Attributes to Smarty functions are much like HTML attributes. Static values don't have to be enclosed in quotes, but it is recommended for literal strings. Variables may also be used, and should not be in quotes.
Some attributes require boolean values (TRUE or FALSE). These can be specified as either unquoted true, on, and yes, or false, off, and no.
Example 3-4. function attribute syntax
{{include file='header.tpl'}}
{{include file='header.tpl' attrib_name='attrib value'}}
{{include file=$includeFile}}
{{include file=#includeFile# title='Smarty is cool'}}
{{html_select_date display_days=yes}}
{{mailto address='smarty@example.com'}}
<select name='company_id'>
{{html_options options=$companies selected=$company_id}}
</select>
Smarty will recognize assigned variables embedded in "double quotes" so long as the variable name contains only numbers, letters, under_scores and brackets[]. See naming for more detail.
With any other characters, for example a .period or $object>reference, then the variable must be surrounded by `backticks`.
You cannot embed modifiers, they must always be applied outside of quotes.
|
Example 3-5. Syntax examples
|
|
Example 3-6. Practical examples
|
Math can be applied directly to variable values.
See also the {{math}} function for complex equations and {{eval}}.
It is sometimes desirable or even necessary to have Smarty ignore sections it would otherwise parse. A classic example is embedding Javascript or CSS code in a template. The problem arises as those languages use the { and } characters which are also the default delimiters for Smarty.
The simplest thing is to avoid the situation altogether by separating your Javascript and CSS code into their own files and then using standard HTML methods to access them.
Including literal content is possible using {{literal}}..{{/literal}} blocks. Similar to HTML entity usage, you can use {{ldelim}},{{rdelim}} or {{$smarty.ldelim}} to display the current delimiters.
Modifiers can be applied to variables, custom functions or strings. To apply a modifier, specify the value followed by a | (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifer name and are separated by a : (colon). Also, all php-functions can be used as modifiers implicitly (more below) and modifiers can be combined.
|
Example 5-1. Modifier examples
|
If you apply a modifier to an array variable instead of a single value variable, the modifier will be applied to every value in that array. If you really want the modifier to work on an entire array as a value, you must prepend the modifier name with a @ symbol.
Example:{$articleTitle|@count} - will print out the number of elements in the $articleTitle array using the php count() function as a modifier.
Modifiers are autoloaded from the $plugins_dir or can be registered explicitly with the register_modifier() function. The later is useful for sharing a function between php scripts and smarty templates.
All php-functions can be used as modifiers implicitly, as demonstrated in the example above. However, using php-functions as modifiers has two little pitfalls:
First - sometimes the order of the function-parameters is not the desirable one. Formatting $foo with {"%2.f"|sprintf:$foo} actually works, but asks for the more intuitive, like {$foo|string_format:"%2.f"} that is provided by the Smarty distribution.
Secondly - if $security is enabled, all php-functions that are to be used as modifiers have to be declared trusted in the MODIFIER_FUNCS element of the $security_settings array
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."} |