PHP ============================= $tpl->assign("foo","bar"); TEMPLATE ============================= { $foo } OUTPUT ============================= bar
PHP ============================= $foo = array("apples", "oranges", "bananas"); $tpl->assign("foo", $foo); TEMPLATE ============================= { $foo } { $foo[0] } { $foo.2 } { assign var=key value=2 } { $foo[$key] } { $foo.$key } OUTPUT ============================= Array apples bananas oranges oranges
PHP ============================= $foo = array("fruit" => "apples", "vegetable" => "carrot", "dairy" => "milk"); $tpl->assign("foo", $foo); TEMPLATE ============================= { $foo.fruit } { $foo.dairy } { $foo.vegetable } OUTPUT ============================= apples milk carrot
PHP ============================= $foo = array("apples", "carrot", "milk"); $tpl->assign("foo", $foo); TEMPLATE ============================= {section name=item loop=$foo} id: {$foo[item]} {/section} OUTPUT ============================= apples carrot milk
After you have loaded a config file, you might want to reference the variables you have loaded from your template. Config variables are more like static constants in that they cannot be changed from the template and are thus referred to differently than normal variables. They are offset by hash or pound marks (#) on either side of the variable.
CONFIG FILE (config.ini) ============================= foo = "bar" test = "this is a string" TEMPLATE ============================= { config_load file="config.ini" } { #foo# } OUTPUT ============================= bar
Variables in quotes are handled exactly the same in Smarty as they are in PHP. Here are some examples:
{ "test $foo test" } <- sees $foo { "test $foo_bar test" } <- sees $foo_bar { "test $foo[0] test" } <- sees $foo[0] { "test $foo[bar] test" } <- sees $foo[bar] { "test $foo.bar test" } <- sees $foo[bar] { "test `$foo.bar` test" } <- sees $foo { 'test $foo test' } <- will interpret literally
The are two ways to concatenate variables using Template Lite. The first is using the standard PHP-compliant 'dot' syntax. The other is to use a comma syntax. Here are some examples of each way.
PHP ============================= $tpl->assign("foo","bar"); $tpl->assign("name","Paul"); TEMPLATE ============================= { $foo.$name } { $foo." ".$name } { $foo, $name } { $foo, " ", $name } OUTPUT ============================= barPaul bar Paul barPaul bar PaulThe comma concatenation technique works only as a standalone tag, i.e.
{ $foo, $bar }
but not { func arg=$foo, $bar }
, whereas the 'dot' syntax works in all cases where a variable is accepted.
Using the $templatelite variable, you can refer to special environment variables from inside the template.
If you are using the $smarty variable in any of your templates you should change it to $templatelite.
If you would like to maintain compatibility with Smarty you can change the name of the reserved variable to smarty using the "reserved_template_varname" configuration variable.
EXAMPLE ============================= $template_object = new Template_Lite; $template_object->template_dir = "./templates/"; $template_object->compile_dir = "./templates_c/"; $template_object->reserved_template_varname = "smarty";With the above change your templates would use the $smarty variable.
{* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *} { $templatelite[GET][PAGE] } { $templatelite.GET.PAGE }
{* The output captured using the {capture}..{/capture} function *} {* can be accessed using the {$templatelite} variable. *} {* name = the name of the captured block *} { $templatelite[CAPTURE][name] } { $templatelite.CAPTURE.name }
{* The {$templatelite} variable can be used to refer to loaded *} {* config variables. {$templatelite.config.name} is identical to {#name#}. *} {* Examples: {config_load}. *} {* name = the name of the config variable to reference *} { $templatelite[CONFIG][name] } { $templatelite.CONFIG.name }
{* The {$templatelite} variable can be used to refer to loaded *} {* defined constants. *} {* name = the name of the defined constant to reference *} { $templatelite[CONST][name] } { $templatelite.CONST.name }
{* display the value of the cookie "username" *} { $templatelite[COOKIE][username] } { $templatelite.COOKIE.username }
{* display the system environment variable "PATH" *} { $templatelite[ENV][PATH] } { $templatelite.ENV.PATH }
{* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *} { $templatelite[GET][PAGE] } { $templatelite.GET.PAGE }
{* Display the left-delimiter value. *} { $templatelite[LDELIM] } { $templatelite.LDELIM }
{* display the current time in unix epoch form *} { $templatelite[NOW] } { $templatelite.NOW }
{* display the variable "page" from a form (POST) *} { $templatelite[POST][PAGE] } { $templatelite.POST.PAGE }
{* Display the right-delimiter value. *} { $templatelite[RDELIM] } { $templatelite.RDELIM }
{* The {$template} variable can be used to refer to one of *} {* many {section} loop properties *} {* ??? = the name of a property *} { $templatelite[SECTION][???] } { $templatelite.SECTION.??? }
{* display the server variable "SERVER_NAME" *} { $templatelite[SERVER][SERVER_NAME] } { $templatelite.SERVER.SERVER_NAME }
{* display the php session variable "id" *} { $templatelite[SESSION][id] } { $templatelite.SESSION.id }
{* display the name of the template currently being processed. *} { $templatelite[TEMPLATE] } { $templatelite.TEMPLATE }
{* display the version of Template Lite *} { $templatelite[VERSION] } { $templatelite.VERSION }