diff options
Diffstat (limited to 'docs/api/variables')
| -rw-r--r-- | docs/api/variables/assigning.md | 139 | ||||
| -rw-r--r-- | docs/api/variables/config-files.md | 89 | ||||
| -rw-r--r-- | docs/api/variables/objects.md | 106 | ||||
| -rw-r--r-- | docs/api/variables/static-class-methods.md | 39 | ||||
| -rw-r--r-- | docs/api/variables/streams.md | 13 |
5 files changed, 386 insertions, 0 deletions
diff --git a/docs/api/variables/assigning.md b/docs/api/variables/assigning.md new file mode 100644 index 00000000..3aa606b6 --- /dev/null +++ b/docs/api/variables/assigning.md @@ -0,0 +1,139 @@ +# Assigning variables + +Templates start to become really useful once you know how to use variables. + +## Basic assigning +Let's revisit the example from the [basics section](../basics.md). The following script assigns a value to +the 'companyName' variable and renders the template: + +```php +<?php + +use Smarty\Smarty; +$smarty = new Smarty(); + +$smarty->assign('companyName', 'AC & ME Corp.'); + +$smarty->display('footer.tpl'); +``` + +footer.tpl: +```smarty +<small>Copyright {$companyName|escape}</small> +``` + +Smarty will apply the [escape modifier](../designers/language-modifiers/language-modifier-escape.md) +to the value assigned to the variable +`companyName` and replace `{$companyName|escape}` with the result. + +```html +<small>Copyright AC & ME Corp.</small> +``` + +Using `$smarty->assign()` is the most common way of assigning data to templates, but there are several other methods. + +## Appending data to an existing variable +Using `append()`, you can add data to an existing variable, usually an array. + +If you append to a string value, it is converted to an array value and +then appended to. You can explicitly pass name/value pairs, or +associative arrays containing the name/value pairs. If you pass the +optional third parameter of TRUE, the value will be merged with the +current array instead of appended. + +Examples: + +```php +<?php +// This is effectively the same as assign() +$smarty->append('foo', 'Fred'); +// After this line, foo will now be seen as an array in the template +$smarty->append('foo', 'Albert'); + +$array = [1 => 'one', 2 => 'two']; +$smarty->append('X', $array); +$array2 = [3 => 'three', 4 => 'four']; +// The following line will add a second element to the X array +$smarty->append('X', $array2); + +// passing an associative array +$smarty->append(['city' => 'Lincoln', 'state' => 'Nebraska']); +``` + +## Assigning to template objects +When you use a template objects, as explained in [rendering a template](../rendering.md#creating-a-template-object), +you can assign data to the template objects directly instead of assigning it to Smarty. This way, you can use different +sets of data for different templates. + +For example: +```php +<?php + +use Smarty\Smarty; +$smarty = new Smarty(); + +$tplBlue = $smarty->createTemplate('blue.tpl'); +$tplBlue->assign('name', 'The one'); +$tplBlue->display(); + +$tplRed = $smarty->createTemplate('red.tpl'); +$tplRed->assign('name', 'Neo'); +$tplRed->display(); +``` + +## Using data objects +For more complex use cases, Smarty supports the concept of data objects. +Data objects are containers to hold data. Data objects can be attached to templates when creating them. +This allows for fine-grained re-use of data. + +For example: +```php +<?php +use Smarty\Smarty; +$smarty = new Smarty; + +// create a data object +$data = $smarty->createData(); + +// assign variable to the data object +$data->assign('name', 'Neo'); + +// create template object which will use variables from the data object +$tpl = $smarty->createTemplate('index.tpl', $data); + +// display the template +$tpl->display(); +``` + +## Clearing assigned data +When re-using templates, you may need to clear data assigned in a previous run. Use `clearAllAssign()` to +clear the values of all assigned variables on data objects, template objects or the Smarty object. + +Examples: +```php +<?php +// assigning data to the Smarty object +$smarty->assign('Name', 'Fred'); +// ... +$smarty->clearAllAssign(); + +// using a data object +$data = $smarty->createData(); +$data->assign('name', 'Neo'); +// ... +$data->clearAllAssign(); + +// using a template +$tplBlue = $smarty->createTemplate('blue.tpl'); +$tplBlue->assign('name', 'The one'); +// ... +$tplBlue->clearAllAssign(); +``` + +Note that there it's only useful to clear assigned data if you: + +1. repeatedly re-use templates, and +2. the variables used may change on each repetition + +If your script simply runs once and then ends, or you always assign the same variables, clearing assigned data +is of no use.
\ No newline at end of file diff --git a/docs/api/variables/config-files.md b/docs/api/variables/config-files.md new file mode 100644 index 00000000..181733ea --- /dev/null +++ b/docs/api/variables/config-files.md @@ -0,0 +1,89 @@ +# Loading data from config files + +Instead of [assigning data to templates from PHP](assigning.md), you can also +use a config file. + +## Example config file +Config files are best suited to manage template settings +from one file. One example is a multi-language application. +Instead of writing multiple templates to support different languages, +you can write a single template file and load your language dependent strings +from config files. + +Example `lang.en.ini`: +```ini +# global variables +pageTitle = "Main Menu" + +[Customer] +pageTitle = "Customer Info" + +[Login] +pageTitle = "Login" +focus = "username" +Intro = """This is a value that spans more + than one line. you must enclose + it in triple quotes.""" + +``` + +Values of [config file variables](./language-variables/language-config-variables.md) can be in +quotes, but not necessary. You can use either single or double quotes. +If you have a value that spans more than one line, enclose the entire +value with triple quotes \("""\). You can put comments into config +files by any syntax that is not a valid config file syntax. We recommend +using a `#` (hash) at the beginning of the line. + +The example config file above has two sections. Section names are +enclosed in \[brackets\]. Section names can be arbitrary strings not +containing `[` or `]` symbols. The variable at the top is a global +variable. Global variables are always +loaded from the config file. If a particular section is loaded, then the +global variables and the variables from that section are also loaded. If +a variable exists both as a global and in a section, the section +variable is used. + +## Loading a config file + +Config files are loaded into templates with the built-in template +function [`{config_load}`](../../designers/language-builtin-functions/language-function-config-load.md) or by calling +`configLoad()` from PHP: + +```php +<?php +$smarty->configLoad('lang.en.ini'); +``` + +Load a specific section with: + +```php +<?php +$smarty->configLoad('lang.en.ini', 'Customer'); +``` + +Note that the global section will always be loaded. + +## Retrieving config variables in PHP + + +## Loading from a resource +Config files (or resources) are loaded by the same resource facilities +as templates. That means that a config file can also be loaded from a db. See [resources](../resources.md) +for more information. + +## Config overwrite +If you name two variables the same within a section, +the last one will be used unless you call: +```php +<?php +$smarty->setConfigOverwrite(false); +``` +When config overwrite is disabled, Smarty will create arrays of config file variables when it encounters +multiple entries with the same name. + +See also [`{config_load}`](./language-builtin-functions/language-function-config-load.md), +[`$config_overwrite`](../programmers/api-variables/variable-config-overwrite.md), +[`$default_config_handler_func`](../programmers/api-variables/variable-default-config-handler-func.md), +[`getConfigVars()`](../programmers/api-functions/api-get-config-vars.md), +[`clearConfig()`](../programmers/api-functions/api-clear-config.md) and +[`configLoad()`](../programmers/api-functions/api-config-load.md) diff --git a/docs/api/variables/objects.md b/docs/api/variables/objects.md new file mode 100644 index 00000000..1d0d7e33 --- /dev/null +++ b/docs/api/variables/objects.md @@ -0,0 +1,106 @@ +# Objects + +Smarty allows access to PHP [objects](https://www.php.net/object) through +the templates. + +> **Note** +> +> When you assign/register objects to templates, be sure that all +> properties and methods accessed from the template are for presentation +> purposes only. It is very easy to inject application logic through +> objects, and this leads to poor designs that are difficult to manage. +> See the Best Practices section of the Smarty website. + +There are two ways to access them. + +## Assign the object +You can assign objects to a template and access them much like any other assigned variable. + +Example: +```php +<?php +// the object + +class My_Object { + public function meth1($params, $smarty_obj) { + return 'this is my meth1'; + } +} + +// We can also assign objects. assign_by_ref when possible. +$smarty->assign('myobj', new My_Object()); + +$smarty->display('index.tpl'); +``` + +And here's how to access your object in `index.tpl`: + +```smarty +{$myobj->meth1('foo',$bar)} +``` + + + +## Register the object +Registerd objects use a different template syntax. Also, a registered object +can be restricted to certain methods or +properties. However, **a registered object cannot be looped over or +assigned in arrays of objects**, etc. + +If security is enabled, no private methods or functions can be accessed +(beginning with '_'). If a method and property of the same name exist, +the method will be used. + +You can restrict the methods and properties that can be accessed by +listing them in an array as the third registration parameter. + +By default, parameters passed to objects through the templates are +passed the same way [custom functions](../../designers/language-custom-functions/index.md) get +them. An associative array is passed as the first parameter, and the +smarty object as the second. If you want the parameters passed one at a +time for each argument like traditional object parameter passing, set +the fourth registration parameter to FALSE. + +The optional fifth parameter has only effect with `format` being TRUE +and contains a list of methods that should be treated as blocks. That +means these methods have a closing tag in the template +(`{foobar->meth2}...{/foobar->meth2}`) and the parameters to the methods +have the same synopsis as the parameters for +[`block tags`](../extending/block-tags.md): They get the four +parameters `$params`, `$content`, `$smarty` and `&$repeat` and they also +behave like block tags. + +```php +<?php +// the object + +class My_Object { + function meth1($params, $smarty_obj) { + return 'this is my meth1'; + } +} + +$myobj = new My_Object; + +// registering the object +$smarty->registerObject('foobar', $myobj); + +// if we want to restrict access to certain methods or properties, list them +$smarty->registerObject('foobar', $myobj, array('meth1','meth2','prop1')); + +// if you want to use the traditional object parameter format, pass a boolean of false +$smarty->registerObject('foobar', $myobj, null, false); + +$smarty->display('index.tpl'); +``` + +And here's how to access your objects in `index.tpl`: + +```smarty +{* access our registered object *} +{foobar->meth1 p1='foo' p2=$bar} + +{* you can also assign the output *} +{foobar->meth1 p1='foo' p2=$bar assign='output'} +the output was {$output} +``` diff --git a/docs/api/variables/static-class-methods.md b/docs/api/variables/static-class-methods.md new file mode 100644 index 00000000..fd3fea4c --- /dev/null +++ b/docs/api/variables/static-class-methods.md @@ -0,0 +1,39 @@ +# Static Classes + +You can directly access static classes. The syntax is roughly the same as in +PHP. + +> **Note** +> +> Direct access to PHP classes is not recommended. This ties the +> underlying application code structure directly to the presentation, +> and also complicates template syntax. It is recommended to register +> plugins which insulate templates from PHP classes/objects. Use at your +> own discretion. + +## Examples + +**class constant BAR** +```smarty +{assign var=foo value=myclass::BAR} +``` + +**method result** +```smarty +{assign var=foo value=myclass::method()} +``` + +**method chaining** +```smarty +{assign var=foo value=myclass::method1()->method2} +``` + +**property bar of class myclass** +```smarty +{assign var=foo value=myclass::$bar} +``` + +**using Smarty variable bar as class name** +```smarty +{assign var=foo value=$bar::method} +``` diff --git a/docs/api/variables/streams.md b/docs/api/variables/streams.md new file mode 100644 index 00000000..93d5c15e --- /dev/null +++ b/docs/api/variables/streams.md @@ -0,0 +1,13 @@ +# Streams + +You can also use streams to call variables. *{$foo:bar}* will use the +*foo://bar* stream to get the template variable. + +Using a PHP stream for a template variable resource from within a +template. + +```smarty +{$foo:bar} +``` + +See also [`Template Resources`](../resources.md) |
