diff options
Diffstat (limited to 'docs/designers/language-variables')
4 files changed, 462 insertions, 0 deletions
diff --git a/docs/designers/language-variables/language-assigned-variables.md b/docs/designers/language-variables/language-assigned-variables.md new file mode 100644 index 00000000..005dea4a --- /dev/null +++ b/docs/designers/language-variables/language-assigned-variables.md @@ -0,0 +1,142 @@ +Variables assigned from PHP {#language.assigned.variables} +=========================== + +Assigned variables that are referenced by preceding them with a dollar +(`$`) sign. + +PHP code + + + <?php + + $smarty = new Smarty(); + + $smarty->assign('firstname', 'Doug'); + $smarty->assign('lastname', 'Evans'); + $smarty->assign('meetingPlace', 'New York'); + + $smarty->display('index.tpl'); + + ?> + +`index.tpl` source: + + + Hello {$firstname} {$lastname}, glad to see you can make it. + <br /> + {* this will not work as $variables are case sensitive *} + This weeks meeting is in {$meetingplace}. + {* this will work *} + This weeks meeting is in {$meetingPlace}. + + + +This above would output: + + + Hello Doug Evans, glad to see you can make it. + <br /> + This weeks meeting is in . + This weeks meeting is in New York. + + + +Associative arrays {#language.variables.assoc.arrays} +------------------ + +You can also reference associative array variables by specifying the key +after a dot \".\" symbol. + + + <?php + $smarty->assign('Contacts', + array('fax' => '555-222-9876', + 'email' => 'zaphod@slartibartfast.example.com', + 'phone' => array('home' => '555-444-3333', + 'cell' => '555-111-1234') + ) + ); + $smarty->display('index.tpl'); + ?> + + + +`index.tpl` source: + + + {$Contacts.fax}<br /> + {$Contacts.email}<br /> + {* you can print arrays of arrays as well *} + {$Contacts.phone.home}<br /> + {$Contacts.phone.cell}<br /> + + + +this will output: + + + 555-222-9876<br /> + zaphod@slartibartfast.example.com<br /> + 555-444-3333<br /> + 555-111-1234<br /> + + + +Array indexes {#language.variables.array.indexes} +------------- + +You can reference arrays by their index, much like native PHP syntax. + + + <?php + $smarty->assign('Contacts', array( + '555-222-9876', + 'zaphod@slartibartfast.example.com', + array('555-444-3333', + '555-111-1234') + )); + $smarty->display('index.tpl'); + ?> + + + +`index.tpl` source: + + + {$Contacts[0]}<br /> + {$Contacts[1]}<br /> + {* you can print arrays of arrays as well *} + {$Contacts[2][0]}<br /> + {$Contacts[2][1]}<br /> + + + +This will output: + + + 555-222-9876<br /> + zaphod@slartibartfast.example.com<br /> + 555-444-3333<br /> + 555-111-1234<br /> + + + +Objects {#language.variables.objects} +------- + +Properties of [objects](#advanced.features.objects) assigned from PHP +can be referenced by specifying the property name after the `->` symbol. + + + name: {$person->name}<br /> + email: {$person->email}<br /> + + + +this will output: + + + name: Zaphod Beeblebrox<br /> + email: zaphod@slartibartfast.example.com<br /> + + diff --git a/docs/designers/language-variables/language-config-variables.md b/docs/designers/language-variables/language-config-variables.md new file mode 100644 index 00000000..a3683d99 --- /dev/null +++ b/docs/designers/language-variables/language-config-variables.md @@ -0,0 +1,83 @@ +Variables loaded from config files {#language.config.variables} +================================== + +Variables that are loaded from the [config files](#config.files) are +referenced by enclosing them within `#hash_marks#`, or with the smarty +variable [`$smarty.config`](#language.variables.smarty.config). The +later syntax is useful for embedding into quoted attribute values, or +accessing variable values such as \$smarty.config.\$foo. + +Example config file - `foo.conf`: + + + pageTitle = "This is mine" + bodyBgColor = '#eeeeee' + tableBorderSize = 3 + tableBgColor = "#bbbbbb" + rowBgColor = "#cccccc" + + + +A template demonstrating the `#hash#` method: + + + {config_load file='foo.conf'} + <html> + <title>{#pageTitle#}</title> + <body bgcolor="{#bodyBgColor#}"> + <table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}"> + <tr bgcolor="{#rowBgColor#}"> + <td>First</td> + <td>Last</td> + <td>Address</td> + </tr> + </table> + </body> + </html> + + + +A template demonstrating the +[`$smarty.config`](#language.variables.smarty.config) method: + + + {config_load file='foo.conf'} + <html> + <title>{$smarty.config.pageTitle}</title> + <body bgcolor="{$smarty.config.bodyBgColor}"> + <table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}"> + <tr bgcolor="{$smarty.config.rowBgColor}"> + <td>First</td> + <td>Last</td> + <td>Address</td> + </tr> + </table> + </body> + </html> + + + +Both examples would output: + + + <html> + <title>This is mine</title> + <body bgcolor="#eeeeee"> + <table border="3" bgcolor="#bbbbbb"> + <tr bgcolor="#cccccc"> + <td>First</td> + <td>Last</td> + <td>Address</td> + </tr> + </table> + </body> + </html> + + + +Config file variables cannot be used until after they are loaded in from +a config file. This procedure is explained later in this document under +[`{config_load}`](#language.function.config.load). + +See also [variables](#language.syntax.variables) and [\$smarty reserved +variables](#language.variables.smarty) diff --git a/docs/designers/language-variables/language-variable-scopes.md b/docs/designers/language-variables/language-variable-scopes.md new file mode 100644 index 00000000..2ba3f026 --- /dev/null +++ b/docs/designers/language-variables/language-variable-scopes.md @@ -0,0 +1,61 @@ +Variable scopes {#language.variable.scopes} +=============== + +You have the choice to assign variables to the scope of the main Smarty +object, data objects created with [`createData()`](#api.create.data), +and template objects created with +[`createTemplate()`](#api.create.template). These objects can be +chained. A template sees all the variables of its own object and all +variables assigned to the objects in its chain of parent objects. + +By default templates which are rendered by +[`$smarty->display(...)`](#api.display) or +[`$smarty->fetch(...)`](#api.fetch) calls are automatically linked to +the Smarty object variable scope. + +By assigning variables to individual data or template objects you have +full control which variables can be seen by a template. + + + + // assign variable to Smarty object scope + $smarty->assign('foo','smarty'); + + // assign variables to data object scope + $data = $smarty->createData(); + $data->assign('foo','data'); + $data->assign('bar','bar-data'); + + // assign variables to other data object scope + $data2 = $smarty->createData($data); + $data2->assign('bar','bar-data2'); + + // assign variable to template object scope + $tpl = $smarty->createTemplate('index.tpl'); + $tpl->assign('bar','bar-template'); + + // assign variable to template object scope with link to Smarty object + $tpl2 = $smarty->createTemplate('index.tpl',$smarty); + $tpl2->assign('bar','bar-template2'); + + // This display() does see $foo='smarty' from the $smarty object + $smarty->display('index.tpl'); + + // This display() does see $foo='data' and $bar='bar-data' from the data object $data + $smarty->display('index.tpl',$data); + + // This display() does see $foo='data' from the data object $data + // and $bar='bar-data2' from the data object $data2 + $smarty->display('index.tpl',$data2); + + // This display() does see $bar='bar-template' from the template object $tpl + $tpl->display(); // or $smarty->display($tpl); + + // This display() does see $bar='bar-template2' from the template object $tpl2 + // and $foo='smarty' form the Smarty object $foo + $tpl2->display(); // or $smarty->display($tpl2); + + + +See also [`assign()`](#api.assign), [`createData()`](#api.create.data) +and [`createTemplate()`](#api.create.template). diff --git a/docs/designers/language-variables/language-variables-smarty.md b/docs/designers/language-variables/language-variables-smarty.md new file mode 100644 index 00000000..f9aa2330 --- /dev/null +++ b/docs/designers/language-variables/language-variables-smarty.md @@ -0,0 +1,176 @@ +{\$smarty} reserved variable {#language.variables.smarty} +============================ + +The PHP reserved `{$smarty}` variable can be used to access several +environment and request variables. The full list of them follows. + +Request variables {#language.variables.smarty.request} +----------------- + +The [request variables](&url.php-manual;reserved.variables) such as +`$_GET`, `$_POST`, `$_COOKIE`, `$_SERVER`, `$_ENV` and `$_SESSION` can +be accessed as demonstrated in the examples below: + + + {* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *} + {$smarty.get.page} + + {* display the variable "page" from a form ($_POST['page']) *} + {$smarty.post.page} + + {* display the value of the cookie "username" ($_COOKIE['username']) *} + {$smarty.cookies.username} + + {* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*} + {$smarty.server.SERVER_NAME} + + {* display the system environment variable "PATH" *} + {$smarty.env.PATH} + + {* display the php session variable "id" ($_SESSION['id']) *} + {$smarty.session.id} + + {* display the variable "username" from merged get/post/cookies/server/env *} + {$smarty.request.username} + + + +> **Note** +> +> For historical reasons `{$SCRIPT_NAME}` is short-hand for +> `{$smarty.server.SCRIPT_NAME}`. +> +> +> <a href="{$SCRIPT_NAME}?page=smarty">click me</a> +> <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a> + +> **Note** +> +> Although Smarty provides direct access to PHP super globals for +> convenience, it should be used with caution. Directly accessing super +> globals mixes underlying application code structure with templates. A +> good practice is to assign specific needed values to template vars. + +{\$smarty.now} {#language.variables.smarty.now} +-------------- + +The current [timestamp](&url.php-manual;function.time) can be accessed +with `{$smarty.now}`. The value reflects the number of seconds passed +since the so-called Epoch on January 1, 1970, and can be passed directly +to the [`date_format`](#language.modifier.date.format) modifier for +display. Note that [`time()`](&url.php-manual;function.time) is called +on each invocation; eg a script that takes three seconds to execute with +a call to `$smarty.now` at start and end will show the three second +difference. + +::: {.informalexample} + + {* use the date_format modifier to show current date and time *} + {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} + + +::: + +{\$smarty.const} {#language.variables.smarty.const} +---------------- + +You can access PHP constant values directly. See also [smarty +constants](#smarty.constants). + +::: {.informalexample} + + <?php + // the constant defined in php + define('MY_CONST_VAL','CHERRIES'); + ?> +::: + +Output the constant in a template with + +::: {.informalexample} + + {$smarty.const.MY_CONST_VAL} +::: + +> **Note** +> +> Although Smarty provides direct access to PHP constants for +> convenience, it is typically avoided as this is mixing underlying +> application code structure into the templates. A good practice is to +> assign specific needed values to template vars. + +{\$smarty.capture} {#language.variables.smarty.capture} +------------------ + +Template output captured via the built-in +[`{capture}..{/capture}`](#language.function.capture) function can be +accessed using the `{$smarty.capture}` variable. See the +[`{capture}`](#language.function.capture) page for more information. + +{\$smarty.config} {#language.variables.smarty.config} +----------------- + +`{$smarty.config}` variable can be used to refer to loaded [config +variables](#language.config.variables). `{$smarty.config.foo}` is a +synonym for `{#foo#}`. See the +[{config\_load}](#language.function.config.load) page for more info. + +{\$smarty.section} {#language.variables.smarty.loops} +------------------ + +The `{$smarty.section}` variables can be used to refer to +[`{section}`](#language.function.section) loop properties. These have +some very useful values such as `.first`, `.index`, etc. + +> **Note** +> +> The `{$smarty.foreach}` variable is no longer used with the new +> [`{foreach}`](#language.function.foreach) syntax, but is still +> supported with Smarty 2.x style foreach syntax. + +{\$smarty.template} {#language.variables.smarty.template} +------------------- + +Returns the name of the current template being processed (without the +directory). + +{\$smarty.template\_object} {#language.variables.smarty.template_object} +--------------------------- + +Returns the template object of the current template being processed. + +{\$smarty.current\_dir} {#language.variables.smarty.current_dir} +----------------------- + +Returns the name of the directory for the current template being +processed. + +{\$smarty.version} {#language.variables.smarty.version} +------------------ + +Returns the version of Smarty the template was compiled with. + + + <div id="footer">Powered by Smarty {$smarty.version}</div> + +{\$smarty.block.child} {#language.variables.smarty.block.child} +---------------------- + +Returns block text from child template. See [Template +interitance](#advanced.features.template.inheritance). + +{\$smarty.block.parent} {#language.variables.smarty.block.parent} +----------------------- + +Returns block text from parent template. See [Template +interitance](#advanced.features.template.inheritance) + +{\$smarty.ldelim}, {\$smarty.rdelim} {#language.variables.smarty.ldelim} +------------------------------------ + +These variables are used for printing the left-delimiter and +right-delimiter value literally, the same as +[`{ldelim},{rdelim}`](#language.function.ldelim). + +See also [assigned variables](#language.assigned.variables) and [config +variables](#language.config.variables) |
