diff options
Diffstat (limited to 'vendor/smarty/smarty/docs/api/extending')
-rw-r--r-- | vendor/smarty/smarty/docs/api/extending/block-tags.md | 59 | ||||
-rw-r--r-- | vendor/smarty/smarty/docs/api/extending/extensions.md | 101 | ||||
-rw-r--r-- | vendor/smarty/smarty/docs/api/extending/introduction.md | 10 | ||||
-rw-r--r-- | vendor/smarty/smarty/docs/api/extending/modifiers.md | 27 | ||||
-rw-r--r-- | vendor/smarty/smarty/docs/api/extending/tags.md | 84 |
5 files changed, 281 insertions, 0 deletions
diff --git a/vendor/smarty/smarty/docs/api/extending/block-tags.md b/vendor/smarty/smarty/docs/api/extending/block-tags.md new file mode 100644 index 000000000..2ae6e77b0 --- /dev/null +++ b/vendor/smarty/smarty/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 +<?php + +function smarty_block_translate($params, $content, \Smarty\Template $template, &$repeat) { + // only output on the closing tag + if (!$repeat){ + if (isset($content)) { + $lang = $params['lang']; + // do some intelligent translation thing here with $content + return $translation; + } + } +} + +$smarty->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/vendor/smarty/smarty/docs/api/extending/extensions.md b/vendor/smarty/smarty/docs/api/extending/extensions.md new file mode 100644 index 000000000..5c87d4dc6 --- /dev/null +++ b/vendor/smarty/smarty/docs/api/extending/extensions.md @@ -0,0 +1,101 @@ +# Creating an extension + +## Default extensions + +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()`. + +## Writing your own extension + +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 +<?php + +use Smarty\Extension\Base; + +class MyExtension extends Base { + + public function getModifierCompiler(string $modifier): ?\Smarty\Compile\Modifier\ModifierCompilerInterface { + + switch ($modifier) { + case 'array_escape': return new MyArrayEscapeModifierCompiler(); + case 'array_unescape': return new MyArrayUnescapeModifierCompiler(); + } + + return null; + } +} + +``` +Another example, that would allow you to use any valid PHP callable as a modifier in your templates: + +```php +<?php + +use Smarty\Extension\Base; + +class MyCallablePassThroughExtension extends Base { + + public function getModifierCallback(string $modifierName) { + + if (is_callable($modifierName)) { + return $modifierName; + } + + return null; + } +} + +``` + +Writing an extension allows you to add a group of tags, block tags and modifiers to the Smarty language. +It also allows you to register pre-, post- and output-filters in a structured way. +The files in `src/Extension/` in the `smarty/smarty` dir should give you all the information you need to start +writing your own extension. + +## Registering an extension + +When you have written your extension, add it to a Smarty instance as follows: + +```php +<?php + +use Smarty\Smarty; + +$smarty = new Smarty(); + +$smarty->addExtension(new MyCustomExtension()); +``` + +This will add `MyCustomExtension` to the end of the extension list, meaning that you cannot override tags or modifiers +from one of Smarty's default extensions. + +Should you wish to insert your extension at the top of the extension list, or create a very limited Smarty version that +only contains the core extension, you can use `Smarty\Smarty::setExtensions()` to override the list of extensions. + +```php +<?php + +use Smarty\Smarty; + +$smarty = new Smarty(); + +$smarty->setExtensions([ + new Smarty\Extension\CoreExtension(), + new MyCustomExtension(), + new Smarty\Extension\DefaultExtension(), +]); +```
\ No newline at end of file diff --git a/vendor/smarty/smarty/docs/api/extending/introduction.md b/vendor/smarty/smarty/docs/api/extending/introduction.md new file mode 100644 index 000000000..69cbce2ac --- /dev/null +++ b/vendor/smarty/smarty/docs/api/extending/introduction.md @@ -0,0 +1,10 @@ +# Extending Smarty + +By default, Smarty is already very complete and powerful. However, you can unlock its real potential by +extending Smarty. + +There are various ways to extend Smarty for it to suit your needs. You can create custom +[tags](tags.md), [block tags](block-tags.md) and [modifiers](modifiers.md) by registering a method as a plugin. + +If this becomes too messy, you can group your custom tags, modifiers, and more into an [Extension](extensions.md). + diff --git a/vendor/smarty/smarty/docs/api/extending/modifiers.md b/vendor/smarty/smarty/docs/api/extending/modifiers.md new file mode 100644 index 000000000..78540cf97 --- /dev/null +++ b/vendor/smarty/smarty/docs/api/extending/modifiers.md @@ -0,0 +1,27 @@ +# Custom modifiers + +Modifiers are little functions that are applied +to a variable in the template before it is displayed or used in some +other context. Smarty comes with a bunch of [modifiers](../../designers/language-modifiers/index.md), but you can +easily add your own. + +In order to do so, you must write a function that accepts as its first parameter 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 the result of its processing. + +For example: +```php +<?php + +function smarty_modifier_substr($string, $offset, $length) { + return substr($string, $offset, $length); +} + +$smarty->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/vendor/smarty/smarty/docs/api/extending/tags.md b/vendor/smarty/smarty/docs/api/extending/tags.md new file mode 100644 index 000000000..38cd4e923 --- /dev/null +++ b/vendor/smarty/smarty/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 +<?php + +function smarty_tag_eightball($params, \Smarty\Template $template): string { + $answers = [ + 'Yes', + 'No', + 'No way', + 'Outlook not so good', + 'Ask again soon', + 'Maybe in your reality' + ]; + + $result = array_rand($answers); + return $answers[$result]; +} + +$smarty->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 +<?php + +function smarty_compiler_tplheader($params, Smarty $smarty) { + return "<?php\necho '" . $smarty->_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 +<?php +echo 'index.tpl compiled at 2023-02-20 20:02'; +``` |