diff --git a/docs/api/extending/block-tags.md b/docs/api/extending/block-tags.md index e69de29b..2ae6e77b 100644 --- a/docs/api/extending/block-tags.md +++ b/docs/api/extending/block-tags.md @@ -0,0 +1,59 @@ +# Custom block tags + +Block tags are tags of the form: `{func} .. {/func}`. In other +words, they enclose a template block and operate on the contents of this +block. + +Block functions take precedence over normal tags of the same name, that is, you +cannot have both custom tag `{func}` and block tag `{func}..{/func}`. + +- By default, your function implementation is called twice by Smarty: + once for the opening tag, and once for the closing tag. (See + `$repeat` below on how to change this.) + +- Only the opening tag of the block has attributes. All attributes are contained in the `$params` + variable as an associative array. The opening tag attributes are + also accessible to your function when processing the closing tag. + +- The value of the `$content` variable depends on whether your + function is called for the opening or closing tag. In case of the + opening tag, it will be NULL, and in case of the closing tag it will + be the contents of the template block. Note that the template block + will have already been processed by Smarty, so all you will receive + is the template output, not the template source. + +- The parameter `$repeat` is passed by reference to the function + implementation and provides a possibility for it to control how many + times the block is displayed. By default `$repeat` is TRUE at the + first call of the block function (the opening tag) and FALSE on all + subsequent calls to the block function (the block's closing tag). + Each time the function implementation returns with `$repeat` being + TRUE, the contents between `{func}...{/func}` are evaluated and the + function implementation is called again with the new block contents + in the parameter `$content`. + +Example: +```php +registerPlugin(Smarty\Smarty::PLUGIN_BLOCK, 'translate', 'smarty_block_translate'); +``` + +This can now be used in your templates as follows: + +```smarty +{translate lang='nl'} + Quia omnis nulla omnis iusto est id et. +{/translate} +``` diff --git a/docs/api/extending/extensions.md b/docs/api/extending/extensions.md index e69de29b..5a5c5a91 100644 --- a/docs/api/extending/extensions.md +++ b/docs/api/extending/extensions.md @@ -0,0 +1,63 @@ +# Creating an extension + +In order to organize your custom tags and modifiers, you can create an Extension. +In fact, most of Smarty itself is organized into two extensions: + +- the core extension, which provides the basic language tags such as `{if}`, `{for}` and `{assign}`. +- the default extension, which provides all default modifiers such as `|escape`, `|nl2br` and `|number_format` + and tags such as `{html_image}`, `{mailto}` and `{textformat}` that are enabled by default, but not necessarily universal. + +> ** Note ** +> +> There is also the 'BCPluginsAdapter' extension, which does not add any new functionality, but +> wraps calls to deprecated methods such as `Smarty\Smarty::addPluginsDir()` and `Smarty\Smarty::loadFilter()`. + +In order to write your own custom extension, you must write a class that implements `Smarty\Extension\ExtensionInterface`. +However, it is usually easier to extend `Smarty\Extension\Base` which provides empty implementation for each of the methods +required by `Smarty\Extension\ExtensionInterface`. This allows you to only override the method(s) you need. + +Example: +```php +registerPlugin(Smarty\Smarty::PLUGIN_MODIFIER, 'substr', 'smarty_modifier_substr'); +``` + +You can now use this in your templates as follows: +```smarty +{$applicationName|substr:0:20} +``` diff --git a/docs/api/extending/tags.md b/docs/api/extending/tags.md index e69de29b..38cd4e92 100644 --- a/docs/api/extending/tags.md +++ b/docs/api/extending/tags.md @@ -0,0 +1,84 @@ +# Custom tags + +You can add your own tags to the Smarty language. + +## Runtime tags + +Usually, you'll add a runtime tag. Adding a runtime tag requires you to provide a callback function that accepts +two parameters: + +- `$params`: all attributes from the template as an associative array. +- `$template`: a `Smarty\Template` object representing the template where tag was used. + +The output (return value) of the function will be substituted in place +of the tag in the template. + +If the function needs to assign some variables to the template or use +some other Smarty-provided functionality, it can use the supplied +`$template` object to do so. + +```php +registerPlugin(Smarty\Smarty::PLUGIN_FUNCTION, 'eightball', 'smarty_tag_eightball'); +``` + +Which can now be used in the template as: + +```smarty +Question: Will we ever have time travel? +Answer: {eightball}. +``` + +## Compiler tags + +Compiler tags are called only during compilation of the template. + +They are useful for injecting PHP code or time-sensitive static content +into the template. If there is both a compiler function and a runtime tag registered under the same name, +the compiler function has precedence. + +The compiler function is passed two parameters: the params array which +contains precompiled strings for the attribute values and the Smarty +object. It's supposed to return the code to be injected into the +compiled template including the surrounding PHP tags. + +Example: +```php +_current_file . " compiled at " . date('Y-m-d H:M'). "';\n?>"; +} + +$smarty->registerPlugin(Smarty\Smarty::PLUGIN_COMPILER, 'tplheader', 'smarty_compiler_tplheader'); +``` + +This function can be called from the template as: + +```smarty +{* this function gets executed at compile time only *} +{tplheader} +``` + +The resulting PHP code in the compiled template would be something like +this: + +```php +registerResource('helloworld', new HelloWorldResource()); ``` +If a Resource's templates should not be run through the Smarty +compiler, the Custom Resource may extend `\Smarty\Resource\UncompiledPlugin`. +The Resource Handler must then implement the function +`renderUncompiled(\Smarty\Template $_template)`. `$_template` is +a reference to the current template and contains all assigned variables +which the implementor can access via +`$_template->getSmarty()->getTemplateVars()`. These Resources simply echo +their rendered content to the output stream. The rendered output will be +output-cached if the Smarty instance was configured accordingly. See +`src/Resource/PhpPlugin.php` for an example. + +If the Resource's compiled templates should not be cached on disk, the +Custom Resource may extend `\Smarty\Resource\RecompiledPlugin`. These Resources +are compiled every time they are accessed. This may be an expensive +overhead. See `src/Resource/StringEval.php` for an +example. + ## Changing the default resource type The default resource type is `file`. If you want to change it, use `Smarty::setDefaultResourceType`. diff --git a/docs/api/security.md b/docs/api/security.md index e69de29b..07120afd 100644 --- a/docs/api/security.md +++ b/docs/api/security.md @@ -0,0 +1,119 @@ +# Security + +Security is good for situations when you have untrusted parties editing +the templates, and you want to reduce the risk of system +security compromises through the template language. + +The settings of the security policy are defined by overriding public properties of an +instance of the \Smarty\Security class. These are the possible settings: + +- `$secure_dir` is an array of template directories that are + considered secure. A directory configured using `$smarty->setTemplateDir()` is + considered secure implicitly. The default is an empty array. +- `$trusted_uri` is an array of regular expressions matching URIs that + are considered trusted. This security directive is used by + [`{fetch}`](../designers/language-custom-functions/language-function-fetch.md) and + [`{html_image}`](../designers/language-custom-functions/language-function-html-image.md). URIs passed to + these functions are reduced to `{$PROTOCOL}://{$HOSTNAME}` to allow + simple regular expressions (without having to deal with edge cases + like authentication-tokens). + + The expression `'#https?://.*smarty.net$#i'` would allow accessing + the following URIs: + + - `http://smarty.net/foo` + - `http://smarty.net/foo` + - `http://www.smarty.net/foo` + - `http://smarty.net/foo` + - `https://foo.bar.www.smarty.net/foo/bla?blubb=1` + + but deny access to these URIs: + + - `http://smarty.com/foo` (not matching top-level domain \"com\") + - `ftp://www.smarty.net/foo` (not matching protocol \"ftp\") + - `http://www.smarty.net.otherdomain.com/foo` (not matching end of + domain \"smarty.net\") + +- `$static_classes` is an array of classes that are considered + trusted. The default is an empty array which allows access to all + static classes. To disable access to all static classes set + $static_classes = null. + +- `$streams` is an array of streams that are considered trusted and + can be used from within template. To disable access to all streams + set $streams = null. An empty array ( $streams = [] ) will + allow all streams. The default is array('file'). + +- `$allowed_modifiers` is an array of (registered / autoloaded) + modifiers that should be accessible to the template. If this array + is non-empty, only the herein listed modifiers may be used. This is + a whitelist. + +- `$disabled_modifiers` is an array of (registered / autoloaded) + modifiers that may not be accessible to the template. + +- `$allowed_tags` is a boolean flag which controls if constants can + function-, block and filter plugins that should be accessible to the + template. If this array is non-empty, only the herein listed + modifiers may be used. This is a whitelist. + +- `$disabled_tags` is an array of (registered / autoloaded) function-, + block and filter plugins that may not be accessible to the template. + +- `$allow_constants` is a boolean flag which controls if constants can + be accessed by the template. The default is "true". + +- `$allow_super_globals` is a boolean flag which controls if the PHP + super globals can be accessed by the template. The default is + "true". + +If security is enabled, no private methods, functions or properties of +static classes or assigned objects can be accessed (beginning with +'_') by the template. + +To customize the security policy settings you can extend the +\Smarty\Security class or create an instance of it. + +```php +enableSecurity('My_Security_Policy'); +``` + +```php +allow_constants = false; + +$smarty->enableSecurity($my_security_policy); +``` + +```php +enableSecurity(); +``` + +> **Note** +> +> Most security policy settings are only checked when the template gets +> compiled. For that reason you should delete all cached and compiled +> template files when you change your security settings. diff --git a/docs/programmers/advanced-features/advanced-features-security.md b/docs/programmers/advanced-features/advanced-features-security.md deleted file mode 100644 index c3dea45b..00000000 --- a/docs/programmers/advanced-features/advanced-features-security.md +++ /dev/null @@ -1,113 +0,0 @@ -# Security - -Security is good for situations when you have untrusted parties editing -the templates e.g. via ftp, and you want to reduce the risk of system -security compromises through the template language. - -The settings of the security policy are defined by properties of an -instance of the \Smarty\Security class. These are the possible settings: - -- `$secure_dir` is an array of template directories that are - considered secure. A [template dir](../../api/configuring.md#setting-the-template-path--s-) is - considered secure implicitly. The default is an empty array. - -- `$trusted_uri` is an array of regular expressions matching URIs that - are considered trusted. This security directive used by - [`{fetch}`](../../designers/language-custom-functions/language-function-fetch.md) and - [`{html_image}`](../../designers/language-custom-functions/language-function-html-image.md). URIs passed to - these functions are reduced to `{$PROTOCOL}://{$HOSTNAME}` to allow - simple regular expressions (without having to deal with edge cases - like authentication-tokens). - - The expression `'#https?://.*smarty.net$#i'` would allow accessing - the following URIs: - - - `http://smarty.net/foo` - - `http://smarty.net/foo` - - `http://www.smarty.net/foo` - - `http://smarty.net/foo` - - `https://foo.bar.www.smarty.net/foo/bla?blubb=1` - - but deny access to these URIs: - - - `http://smarty.com/foo` (not matching top-level domain \"com\") - - `ftp://www.smarty.net/foo` (not matching protocol \"ftp\") - - `http://www.smarty.net.otherdomain.com/foo` (not matching end of - domain \"smarty.net\") - -- `$static_classes` is an array of classes that are considered - trusted. The default is an empty array which allows access to all - static classes. To disable access to all static classes set - $static_classes = null. - -- `$streams` is an array of streams that are considered trusted and - can be used from within template. To disable access to all streams - set $streams = null. An empty array ( $streams = [] ) will - allow all streams. The default is array('file'). - -- `$allowed_modifiers` is an array of (registered / autoloaded) - modifiers that should be accessible to the template. If this array - is non-empty, only the herein listed modifiers may be used. This is - a whitelist. - -- `$disabled_modifiers` is an array of (registered / autoloaded) - modifiers that may not be accessible to the template. - -- `$allowed_tags` is a boolean flag which controls if constants can - function-, block and filter plugins that should be accessible to the - template. If this array is non-empty, only the herein listed - modifiers may be used. This is a whitelist. - -- `$disabled_tags` is an array of (registered / autoloaded) function-, - block and filter plugins that may not be accessible to the template. - -- `$allow_constants` is a boolean flag which controls if constants can - be accessed by the template. The default is "true". - -- `$allow_super_globals` is a boolean flag which controls if the PHP - super globals can be accessed by the template. The default is - "true". - -If security is enabled, no private methods, functions or properties of -static classes or assigned objects can be accessed (beginning with -'_') by the template. - -To customize the security policy settings you can extend the -\Smarty\Security class or create an instance of it. - -```php -enableSecurity('My_Security_Policy'); -``` - -```php -allow_constants = false; -// enable security -$smarty->enableSecurity($my_security_policy); -``` - -```php -enableSecurity(); -``` - -> **Note** -> -> Most security policy settings are only checked when the template gets -> compiled. For that reason you should delete all cached and compiled -> template files when you change your security settings. diff --git a/docs/programmers/plugins.md b/docs/programmers/plugins.md deleted file mode 100644 index a1ed299c..00000000 --- a/docs/programmers/plugins.md +++ /dev/null @@ -1,43 +0,0 @@ -Extending Smarty With Plugins {#plugins} -============================= - -## Table of contents - -- [How Plugins Work](./plugins/plugins-howto.md) -- [Naming Conventions](./plugins/plugins-naming-conventions.md) -- [Writing Plugins](./plugins/plugins-writing.md) -- [Template Functions](./plugins/plugins-functions.md) -- [Modifiers](./plugins/plugins-modifiers.md) -- [Block Functions](./plugins/plugins-block-functions.md) -- [Compiler Functions](./plugins/plugins-compiler-functions.md) -- [Prefilters/Postfilters](./plugins/plugins-prefilters-postfilters.md) -- [Output Filters](./plugins/plugins-outputfilters.md) -- [Resources](./plugins/plugins-resources.md) - -Version 2.0 introduced the plugin architecture that is used for almost -all the customizable functionality of Smarty. This includes: - -- functions - -- modifiers - -- block functions - -- compiler functions - -- prefilters - -- postfilters - -- outputfilters - -- resources - -- inserts - -With the exception of resources, backwards compatibility with the old -way of registering handler functions via register\_\* API is preserved. -If you did not use the API but instead modified the class variables -`$custom_funcs`, `$custom_mods`, and other ones directly, then you will -need to adjust your scripts to either use the API or convert your custom -functionality into plugins. diff --git a/docs/programmers/plugins/plugins-block-functions.md b/docs/programmers/plugins/plugins-block-functions.md deleted file mode 100644 index ca454fd4..00000000 --- a/docs/programmers/plugins/plugins-block-functions.md +++ /dev/null @@ -1,89 +0,0 @@ -Block Functions {#plugins.block.functions} -=============== - -void - -smarty\_block\_ - -name - -array - -\$params - -mixed - -\$content - -object - -\$template - -boolean - -&\$repeat - -Block functions are functions of the form: `{func} .. {/func}`. In other -words, they enclose a template block and operate on the contents of this -block. Block functions take precedence over [custom -functions](#language.custom.functions) of the same name, that is, you -cannot have both custom function `{func}` and block function -`{func}..{/func}`. - -- By default your function implementation is called twice by Smarty: - once for the opening tag, and once for the closing tag. (See - `$repeat` below on how to change this.) - -- Starting with Smarty 3.1 the returned value of the opening tag call - is displayed as well. - -- Only the opening tag of the block function may have - [attributes](#language.syntax.attributes). All attributes passed to - template functions from the template are contained in the `$params` - variable as an associative array. The opening tag attributes are - also accessible to your function when processing the closing tag. - -- The value of the `$content` variable depends on whether your - function is called for the opening or closing tag. In case of the - opening tag, it will be NULL, and in case of the closing tag it will - be the contents of the template block. Note that the template block - will have already been processed by Smarty, so all you will receive - is the template output, not the template source. - -- The parameter `$repeat` is passed by reference to the function - implementation and provides a possibility for it to control how many - times the block is displayed. By default `$repeat` is TRUE at the - first call of the block-function (the opening tag) and FALSE on all - subsequent calls to the block function (the block\'s closing tag). - Each time the function implementation returns with `$repeat` being - TRUE, the contents between `{func}...{/func}` are evaluated and the - function implementation is called again with the new block contents - in the parameter `$content`. - - - - - -See also: [`registerPlugin()`](#api.register.plugin), -[`unregisterPlugin()`](#api.unregister.plugin). diff --git a/docs/programmers/plugins/plugins-compiler-functions.md b/docs/programmers/plugins/plugins-compiler-functions.md deleted file mode 100644 index ef2454e8..00000000 --- a/docs/programmers/plugins/plugins-compiler-functions.md +++ /dev/null @@ -1,66 +0,0 @@ -Compiler Functions {#plugins.compiler.functions} -================== - -Compiler functions are called only during compilation of the template. -They are useful for injecting PHP code or time-sensitive static content -into the template. If there is both a compiler function and a [custom -function](#language.custom.functions) registered under the same name, -the compiler function has precedence. - -mixed - -smarty\_compiler\_ - -name - -array - -\$params - -object - -\$smarty - -The compiler function is passed two parameters: the params array which -contains precompiled strings for the attribute values and the Smarty -object. It\'s supposed to return the code to be injected into the -compiled template including the surrounding PHP tags. - - - _current_file . " compiled at " . date('Y-m-d H:M'). "';\n?>"; - } - ?> - -This function can be called from the template as: - - - {* this function gets executed at compile time only *} - {tplheader} - - - -The resulting PHP code in the compiled template would be something like -this: - - - - - - -See also [`registerPlugin()`](#api.register.plugin), -[`unregisterPlugin()`](#api.unregister.plugin). diff --git a/docs/programmers/plugins/plugins-functions.md b/docs/programmers/plugins/plugins-functions.md deleted file mode 100644 index 5ac51539..00000000 --- a/docs/programmers/plugins/plugins-functions.md +++ /dev/null @@ -1,94 +0,0 @@ -Template Functions {#plugins.functions} -================== - -void - -smarty\_function\_ - -name - -array - -\$params - -object - -\$template - -All [attributes](#language.syntax.attributes) passed to template -functions from the template are contained in the `$params` as an -associative array. - -The output (return value) of the function will be substituted in place -of the function tag in the template, eg the -[`{fetch}`](#language.function.fetch) function. Alternatively, the -function can simply perform some other task without any output, eg the -[`{assign}`](#language.function.assign) function. - -If the function needs to assign some variables to the template or use -some other Smarty-provided functionality, it can use the supplied -`$template` object to do so eg `$template->foo()`. - - - - -which can be used in the template as: - - Question: Will we ever have time travel? - Answer: {eightball}. - - - - assign($params['var'], $params['value']); - - } - ?> - - - -See also: [`registerPlugin()`](#api.register.plugin), -[`unregisterPlugin()`](#api.unregister.plugin). diff --git a/docs/programmers/plugins/plugins-howto.md b/docs/programmers/plugins/plugins-howto.md deleted file mode 100644 index 5738c3fc..00000000 --- a/docs/programmers/plugins/plugins-howto.md +++ /dev/null @@ -1,18 +0,0 @@ -How Plugins Work {#plugins.howto} -================ - -Plugins are always loaded on demand. Only the specific modifiers, -functions, resources, etc invoked in the templates scripts will be -loaded. Moreover, each plugin is loaded only once, even if you have -several different instances of Smarty running within the same request. - -Pre/postfilters and output filters are a bit of a special case. Since -they are not mentioned in the templates, they must be registered or -loaded explicitly via API functions before the template is processed. -The order in which multiple filters of the same type are executed -depends on the order in which they are registered or loaded. - -The [plugins directory](#variable.plugins.dir) can be a string -containing a path or an array containing multiple paths. To install a -plugin, simply place it in one of the directories and Smarty will use it -automatically. diff --git a/docs/programmers/plugins/plugins-modifiers.md b/docs/programmers/plugins/plugins-modifiers.md deleted file mode 100644 index a4e99daa..00000000 --- a/docs/programmers/plugins/plugins-modifiers.md +++ /dev/null @@ -1,86 +0,0 @@ -Modifiers {#plugins.modifiers} -========= - -[Modifiers](#language.modifiers) are little functions that are applied -to a variable in the template before it is displayed or used in some -other context. Modifiers can be chained together. - -mixed - -smarty\_modifier\_ - -name - -mixed - -\$value - -\[mixed - -\$param1 - -, \...\] - -The first parameter to the modifier plugin is the value on which the -modifier is to operate. The rest of the parameters are optional, -depending on what kind of operation is to be performed. - -The modifier has to [return](https://www.php.net/return) the result of its -processing. - -This plugin basically aliases one of the built-in PHP functions. It does -not have any additional parameters. - - - - - - $length) { - $length -= strlen($etc); - $fragment = substr($string, 0, $length+1); - if ($break_words) - $fragment = substr($fragment, 0, -1); - else - $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); - return $fragment.$etc; - } else - return $string; - } - ?> - - - -See also [`registerPlugin()`](#api.register.plugin), -[`unregisterPlugin()`](#api.unregister.plugin). diff --git a/docs/programmers/plugins/plugins-naming-conventions.md b/docs/programmers/plugins/plugins-naming-conventions.md deleted file mode 100644 index fdfba764..00000000 --- a/docs/programmers/plugins/plugins-naming-conventions.md +++ /dev/null @@ -1,49 +0,0 @@ -Naming Conventions {#plugins.naming.conventions} -================== - -Plugin files and functions must follow a very specific naming convention -in order to be located by Smarty. - -**plugin files** must be named as follows: - -> ` -> type.name.php -> ` - -- Where `type` is one of these plugin types: - - - function - - - modifier - - - block - - - compiler - - - prefilter - - - postfilter - - - outputfilter - - - resource - -- And `name` should be a valid identifier; letters, numbers, and - underscores only, see [php - variables](https://www.php.net/language.variables). - -- Some examples: `function.html_select_date.php`, `resource.db.php`, - `modifier.spacify.php`. - -**plugin functions** inside the PHP files must be named as follows: - -> `smarty_type_name` - -- The meanings of `type` and `name` are the same as above. - -- An example modifier name `foo` would be - `function smarty_modifier_foo()`. - -Smarty will output appropriate error messages if the plugin file it -needs is not found, or if the file or the plugin function are named -improperly. diff --git a/docs/programmers/plugins/plugins-outputfilters.md b/docs/programmers/plugins/plugins-outputfilters.md deleted file mode 100644 index f1e9111d..00000000 --- a/docs/programmers/plugins/plugins-outputfilters.md +++ /dev/null @@ -1,48 +0,0 @@ -Output Filters {#plugins.outputfilters} -============== - -Output filter plugins operate on a template\'s output, after the -template is loaded and executed, but before the output is displayed. - -string - -smarty\_outputfilter\_ - -name - -string - -\$template\_output - -object - -\$template - -The first parameter to the output filter function is the template output -that needs to be processed, and the second parameter is the instance of -Smarty invoking the plugin. The plugin is supposed to do the processing -and return the results. - - - - - - -See also [`registerFilter()`](#api.register.filter), -[`unregisterFilter()`](#api.unregister.filter). diff --git a/docs/programmers/plugins/plugins-prefilters-postfilters.md b/docs/programmers/plugins/plugins-prefilters-postfilters.md deleted file mode 100644 index 97cb7eaa..00000000 --- a/docs/programmers/plugins/plugins-prefilters-postfilters.md +++ /dev/null @@ -1,89 +0,0 @@ -Prefilters/Postfilters {#plugins.prefilters.postfilters} -====================== - -Prefilter and postfilter plugins are very similar in concept; where they -differ is in the execution \-- more precisely the time of their -execution. - -string - -smarty\_prefilter\_ - -name - -string - -\$source - -object - -\$template - -Prefilters are used to process the source of the template immediately -before compilation. The first parameter to the prefilter function is the -template source, possibly modified by some other prefilters. The plugin -is supposed to return the modified source. Note that this source is not -saved anywhere, it is only used for compilation. - -string - -smarty\_postfilter\_ - -name - -string - -\$compiled - -object - -\$template - -Postfilters are used to process the compiled output of the template (the -PHP code) immediately after the compilation is done but before the -compiled template is saved to the filesystem. The first parameter to the -postfilter function is the compiled template code, possibly modified by -other postfilters. The plugin is supposed to return the modified version -of this code. - - - ]+>!e', 'strtolower("$1")', $source); - } - ?> - - - - - \ngetTemplateVars()); ?>\n" . $compiled; - return $compiled; - } - ?> - - - -See also [`registerFilter()`](#api.register.filter) and -[`unregisterFilter()`](#api.unregister.filter). diff --git a/docs/programmers/plugins/plugins-resources.md b/docs/programmers/plugins/plugins-resources.md deleted file mode 100644 index 86a18127..00000000 --- a/docs/programmers/plugins/plugins-resources.md +++ /dev/null @@ -1,130 +0,0 @@ -Resources {#plugins.resources} -========= - -Resource plugins are meant as a generic way of providing template -sources or PHP script components to Smarty. Some examples of resources: -databases, LDAP, shared memory, sockets, and so on. - -Custom Resources may be put in a file `resource.foobarxyz.php` within -your [`$plugins_dir`](#variable.plugins.dir), or registered on runtime -with [`registerResource()`](#api.register.resource). In either case you -will be able to access that resource by prepending its name to the -template you\'re addressing: `foobarxyz:yourtemplate.tpl`. - -If a Resource\'s templates should not be run through the Smarty -compiler, the Custom Resource may extend `\Smarty\Resource\UncompiledPlugin`. -The Resource Handler must then implement the function -`renderUncompiled(\Smarty\Template $_template)`. `$_template` is -a reference to the current template and contains all assigned variables -which the implementor can access via -`$_template->getSmarty()->getTemplateVars()`. These Resources simply echo -their rendered content to the output stream. The rendered output will be -output-cached if the Smarty instance was configured accordingly. See -`src/Resource/PhpPlugin.php` for an example. - -If the Resource\'s compiled templates should not be cached on disk, the -Custom Resource may extend `\Smarty\Resource\RecompiledPlugin`. These Resources -are compiled every time they are accessed. This may be an expensive -overhead. See `src/Resource/StringEval.php` for an -example. - - - CREATE TABLE IF NOT EXISTS `templates` ( - * `name` varchar(100) NOT NULL, - * `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - * `source` text, - * PRIMARY KEY (`name`) - * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - * - * Demo data: - *
INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');- * - - * @author Rodney Rehm - */ - class My_Resource_Mysql extends \Smarty\Resource\CustomPlugin { - // PDO instance - protected $db; - // prepared fetch() statement - protected $fetch; - // prepared fetchTimestamp() statement - protected $mtime; - - public function __construct() { - try { - $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); - } catch (PDOException $e) { - throw new \Smarty\Exception('Mysql Resource failed: ' . $e->getMessage()); - } - $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); - $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); - } - - /** - * Fetch a template and its modification time from database - * - * @param string $name template name - * @param string $source template source - * @param integer $mtime template modification timestamp (epoch) - * @return void - */ - protected function fetch($name, &$source, &$mtime) - { - $this->fetch->execute(array('name' => $name)); - $row = $this->fetch->fetch(); - $this->fetch->closeCursor(); - if ($row) { - $source = $row['source']; - $mtime = strtotime($row['modified']); - } else { - $source = null; - $mtime = null; - } - } - - /** - * Fetch a template's modification time from database - * - * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source. - * @param string $name template name - * @return integer timestamp (epoch) the template was modified - */ - protected function fetchTimestamp($name) { - $this->mtime->execute(array('name' => $name)); - $mtime = $this->mtime->fetchColumn(); - $this->mtime->closeCursor(); - return strtotime($mtime); - } - } - - - - $smarty = new Smarty(); - $smarty->registerResource('mysql', new My_Resource_Mysql()); - - // using resource from php script - $smarty->display("mysql:index.tpl"); - ?> - - - -And from within Smarty template: - - - {include file='mysql:extras/navigation.tpl'} - - - -See also [`registerResource()`](#api.register.resource), -[`unregisterResource()`](#api.unregister.resource). diff --git a/docs/programmers/plugins/plugins-writing.md b/docs/programmers/plugins/plugins-writing.md deleted file mode 100644 index 729f87b8..00000000 --- a/docs/programmers/plugins/plugins-writing.md +++ /dev/null @@ -1,20 +0,0 @@ -Writing Plugins {#plugins.writing} -=============== - -Plugins can be either loaded by Smarty automatically from the filesystem -or they can be registered at runtime via one of the register\_\* API -functions. They can also be unregistered by using unregister\_\* API -functions. - -For the plugins that are registered at runtime, the name of the plugin -function(s) does not have to follow the naming convention. - -As a general rule, the currently evaluated template\'s -Smarty\_Internal\_Template object is always passed to the plugins as the -last parameter with two exceptions: - -- modifiers do not get passed the Smarty\_Internal\_Template object at - all - -- blocks get passed `$repeat` after the Smarty\_Internal\_Template - object to keep backwards compatibility to older versions of Smarty. diff --git a/mkdocs.yml b/mkdocs.yml index abed5d8b..d5a809cb 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -134,9 +134,9 @@ nav: - 'Custom cache storage layers': 'api/caching/custom-storage-layers.md' - 'Extending Smarty': - 'Introduction': 'api/extending/introduction.md' - - 'Creating an Extension': 'api/extending/extensions.md' - 'Custom tags': 'api/extending/tags.md' - 'Custom block tags': 'api/extending/block-tags.md' - 'Custom modifiers': 'api/extending/modifiers.md' + - 'Creating an extension': 'api/extending/extensions.md' - 'Security': 'api/security.md'