├── changelog └── .gitkeep ├── docs ├── _config.yml ├── designers │ ├── language-builtin-functions.md │ ├── language-builtin-functions │ │ ├── language-function-insert.md │ │ ├── language-function-nocache.md │ │ ├── language-function-while.md │ │ ├── language-function-debug.md │ │ ├── language-function-literal.md │ │ ├── language-function-ldelim.md │ │ ├── language-function-setfilter.md │ │ ├── language-function-extends.md │ │ ├── language-function-strip.md │ │ └── index.md │ ├── language-modifiers │ │ ├── language-modifier-is_array.md │ │ ├── language-modifier-raw.md │ │ ├── language-modifier-strlen.md │ │ ├── language-modifier-noprint.md │ │ ├── language-modifier-isset.md │ │ ├── language-modifier-empty.md │ │ ├── language-modifier-str-repeat.md │ │ ├── language-modifier-in_array.md │ │ ├── language-modifier-lower.md │ │ ├── language-modifier-count-words.md │ │ ├── language-modifier-upper.md │ │ ├── language-modifier-count.md │ │ ├── language-modifier-join.md │ │ ├── language-modifier-cat.md │ │ ├── language-modifier-nl2br.md │ │ ├── language-modifier-substr.md │ │ ├── language-modifier-number-format.md │ │ ├── language-modifier-string-format.md │ │ ├── language-modifier-count-sentences.md │ │ ├── language-modifier-debug-print-var.md │ │ ├── language-modifier-count-paragraphs.md │ │ ├── language-modifier-strip.md │ │ ├── language-modifier-json-encode.md │ │ ├── language-modifier-split.md │ │ ├── language-modifier-count-characters.md │ │ ├── language-modifier-round.md │ │ ├── language-modifier-replace.md │ │ ├── language-modifier-spacify.md │ │ ├── language-modifier-default.md │ │ ├── language-modifier-to-charset.md │ │ ├── language-modifier-from-charset.md │ │ ├── language-modifier-strip-tags.md │ │ ├── language-modifier-capitalize.md │ │ └── language-modifier-regex-replace.md │ ├── language-custom-functions │ │ ├── language-function-debug.md │ │ ├── index.md │ │ └── language-function-counter.md │ ├── language-combining-modifiers.md │ ├── language-basic-syntax │ │ ├── index.md │ │ ├── language-syntax-tags.md │ │ └── language-syntax-attributes.md │ └── language-variables │ │ └── index.md ├── programmers │ ├── api-functions │ │ ├── add-extension.md │ │ ├── api-unregister-object.md │ │ ├── api-disable-security.md │ │ ├── api-unregister-filter.md │ │ ├── api-test-install.md │ │ ├── api-unregister-resource.md │ │ ├── api-unregister-cacheresource.md │ │ ├── api-get-plugins-dir.md │ │ ├── api-unregister-plugin.md │ │ ├── api-clear-assign.md │ │ ├── api-clear-all-assign.md │ │ ├── api-clear-config.md │ │ ├── api-mute-expected-errors.md │ │ ├── api-get-config-vars.md │ │ ├── api-get-template-vars.md │ │ ├── api-load-filter.md │ │ ├── api-clear-all-cache.md │ │ ├── api-enable-security.md │ │ ├── api-get-config-dir.md │ │ ├── api-get-registered-object.md │ │ ├── api-register-object.md │ │ ├── api-add-plugins-dir.md │ │ ├── api-register-cacheresource.md │ │ ├── api-set-plugins-dir.md │ │ ├── api-clear-compiled-tpl.md │ │ ├── api-config-load.md │ │ ├── api-register-resource.md │ │ ├── api-create-data.md │ │ ├── api-append.md │ │ ├── api-register-filter.md │ │ ├── api-compile-all-config.md │ │ ├── api-register-class.md │ │ ├── api-clear-cache.md │ │ ├── api-create-template.md │ │ └── api-compile-all-templates.md │ └── api-variables │ │ ├── variable-use-include-path.md │ │ ├── variable-compiler-class.md │ │ ├── variable-compile-locking.md │ │ ├── variable-locking-timeout.md │ │ ├── variable-force-cache.md │ │ ├── variable-debug-template.md │ │ ├── variable-caching-type.md │ │ ├── variable-default-resource-type.md │ │ ├── variable-default-config-type.md │ │ ├── variable-smarty-debug-id.md │ │ ├── variable-default-modifiers.md │ │ ├── variable-config-booleanize.md │ │ ├── variable-cache-locking.md │ │ ├── variable-config-read-hidden.md │ │ ├── variable-auto-literal.md │ │ ├── variable-force-compile.md │ │ ├── variable-cache-modified-check.md │ │ ├── variable-cache-id.md │ │ ├── variable-debugging.md │ │ ├── variable-config-dir.md │ │ ├── variable-debugging-ctrl.md │ │ ├── variable-error-reporting.md │ │ ├── variable-escape-html.md │ │ ├── variable-merge-compiled-includes.md │ │ ├── variable-compile-dir.md │ │ ├── variable-template-dir.md │ │ ├── variable-config-overwrite.md │ │ ├── variable-cache-dir.md │ │ ├── variable-use-sub-dirs.md │ │ ├── variable-cache-lifetime.md │ │ └── variable-compile-id.md ├── img │ ├── favicon.ico │ └── smarty.svg ├── api │ ├── variables │ │ ├── streams.md │ │ └── static-class-methods.md │ ├── extending │ │ ├── introduction.md │ │ └── modifiers.md │ ├── filters │ │ ├── prefilters.md │ │ ├── postfilters.md │ │ └── output-filters.md │ └── caching │ │ └── custom-storage-layers.md └── index.md ├── demo ├── templates │ ├── footer.tpl │ └── header.tpl ├── configs │ └── test.conf └── index.php ├── src ├── Filter │ ├── FilterInterface.php │ └── FilterPluginWrapper.php ├── FunctionHandler │ ├── FunctionHandlerInterface.php │ ├── Base.php │ ├── AttributeFunctionHandlerInterface.php │ ├── BCPluginWrapper.php │ ├── Count.php │ └── Counter.php ├── BlockHandler │ ├── BlockHandlerInterface.php │ ├── Base.php │ └── BlockPluginWrapper.php ├── Exception.php ├── Compiler │ └── BaseCompiler.php ├── UndefinedVariable.php ├── Compile │ ├── Modifier │ │ ├── JsonEncodeModifierCompiler.php │ │ ├── SubstrModifierCompiler.php │ │ ├── NoPrintModifierCompiler.php │ │ ├── BCPluginWrapper.php │ │ ├── StrlenModifierCompiler.php │ │ ├── StrRepeatModifierCompiler.php │ │ ├── ModifierCompilerInterface.php │ │ ├── StringFormatModifierCompiler.php │ │ ├── Nl2brModifierCompiler.php │ │ ├── EmptyModifierCompiler.php │ │ ├── UpperModifierCompiler.php │ │ ├── IsArrayModifierCompiler.php │ │ ├── RawModifierCompiler.php │ │ ├── LowerModifierCompiler.php │ │ ├── CountParagraphsModifierCompiler.php │ │ ├── CatModifierCompiler.php │ │ ├── RoundModifierCompiler.php │ │ ├── IndentModifierCompiler.php │ │ ├── CountSentencesModifierCompiler.php │ │ ├── ToCharsetModifierCompiler.php │ │ ├── CountWordsModifierCompiler.php │ │ ├── FromCharsetModifierCompiler.php │ │ ├── StripTagsModifierCompiler.php │ │ ├── IssetModifierCompiler.php │ │ ├── DefaultModifierCompiler.php │ │ ├── CountCharactersModifierCompiler.php │ │ ├── StripModifierCompiler.php │ │ ├── WordWrapModifierCompiler.php │ │ ├── UnescapeModifierCompiler.php │ │ └── Base.php │ ├── Tag │ │ ├── ContinueTag.php │ │ ├── ElseTag.php │ │ ├── SectionElse.php │ │ ├── BCPluginWrapper.php │ │ ├── ForElse.php │ │ ├── Nocache.php │ │ ├── Rdelim.php │ │ ├── NocacheClose.php │ │ ├── WhileClose.php │ │ ├── Ldelim.php │ │ ├── ForeachElse.php │ │ ├── CaptureClose.php │ │ ├── SetfilterClose.php │ │ ├── IfClose.php │ │ ├── Debug.php │ │ ├── SectionClose.php │ │ ├── ForClose.php │ │ ├── Setfilter.php │ │ ├── ForeachClose.php │ │ ├── Append.php │ │ ├── Inheritance.php │ │ └── ConfigLoad.php │ ├── CompilerInterface.php │ ├── DefaultHandlerBlockCompiler.php │ ├── ObjectMethodBlockCompiler.php │ └── DefaultHandlerFunctionCallCompiler.php ├── Extension │ ├── CallbackWrapper.php │ └── Base.php ├── Template │ └── Config.php ├── ParseTree │ ├── Base.php │ ├── Code.php │ ├── DqContent.php │ └── Text.php ├── Resource │ ├── RecompiledPlugin.php │ └── StreamPlugin.php ├── Runtime │ └── DefaultPluginHandlerRuntime.php └── CompilerException.php ├── run-tests.sh ├── run-tests-for-all-php-versions.sh ├── SECURITY.md ├── README.md ├── Makefile ├── TODO.txt ├── composer.json └── libs └── Smarty.class.php /changelog/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/add-extension.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /demo/templates/footer.tpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-use-include-path.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-insert.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smarty-php/smarty/HEAD/docs/img/favicon.ico -------------------------------------------------------------------------------- /demo/configs/test.conf: -------------------------------------------------------------------------------- 1 | title = Welcome to Smarty! 2 | cutoff_size = 40 3 | 4 | [setup] 5 | bold = true 6 | -------------------------------------------------------------------------------- /demo/templates/header.tpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | {$title} - {$Name} 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/Filter/FilterInterface.php: -------------------------------------------------------------------------------- 1 | Smarty: ' . $this->message . ' <-- '; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-force-cache.md: -------------------------------------------------------------------------------- 1 | \$force\_cache {#variable.force.cache} 2 | ============== 3 | 4 | This forces Smarty to (re)cache templates on every invocation. It does 5 | not override the [`$caching`](#variable.caching) level, but merely 6 | pretends the template has never been cached before. 7 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-noprint.md: -------------------------------------------------------------------------------- 1 | # noprint 2 | 3 | Always returns an empty string. This can be used to call a function or a method on an object that 4 | returns output, and suppress the output. 5 | 6 | ## Basic usage 7 | ```smarty 8 | {$controller->sendEmail()|noprint} 9 | ``` 10 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Runs composer update, echoes php version and runs PHPUnit 4 | # Usage examples: 5 | # - ./run-tests.sh --group 20221124 6 | # - ./run-tests.sh --exclude-group slow 7 | 8 | composer update --quiet 9 | #php -r 'echo "\nPHP version " . phpversion() . ". ";' 10 | php ./vendor/phpunit/phpunit/phpunit $@ 11 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-isset.md: -------------------------------------------------------------------------------- 1 | # isset 2 | 3 | Returns true if the variable(s) passed to it are different from null. 4 | 5 | If multiple parameters are supplied then isset() will return true only if all of the parameters are 6 | not null. 7 | 8 | ## Basic usage 9 | ```smarty 10 | {if $myVar|isset}all set!{/if} 11 | ``` 12 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-unregister-object.md: -------------------------------------------------------------------------------- 1 | unregisterObject() 2 | 3 | dynamically unregister an object 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | unregisterObject 11 | 12 | string 13 | 14 | object\_name 15 | 16 | See also [`registerObject()`](#api.register.object) and [objects 17 | section](#advanced.features.objects) 18 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-disable-security.md: -------------------------------------------------------------------------------- 1 | disableSecurity() 2 | 3 | disables template security 4 | 5 | Description 6 | =========== 7 | 8 | string 9 | 10 | disableSecurity 11 | 12 | This disables security checking on templates. 13 | 14 | See also [`enableSecurity()`](#api.enable.security), and 15 | [Security](#advanced.features.security). 16 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-empty.md: -------------------------------------------------------------------------------- 1 | # empty 2 | 3 | Returns true if var does not exist or has a value that is empty or equal to zero, aka falsey, see conversion to boolean. Otherwise returns false. 4 | 5 | ## Basic usage 6 | 7 | ```smarty 8 | {if $myVar|empty}it's an empty variable{/if} 9 | {if empty($myVar)}it's an empty variable{/if} 10 | ``` 11 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-debug-template.md: -------------------------------------------------------------------------------- 1 | \$debug\_tpl {#variable.debug_template} 2 | ============ 3 | 4 | This is the name of the template file used for the debugging console. By 5 | default, it is named `debug.tpl` and is located in `src/debug.tpl`. 6 | 7 | See also [`$debugging`](#variable.debugging) and the [debugging 8 | console](#chapter.debugging.console) section. 9 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-caching-type.md: -------------------------------------------------------------------------------- 1 | \$caching\_type {#variable.caching.type} 2 | =============== 3 | 4 | This property specifies the name of the caching handler to use. It 5 | defaults to `file`, enabling the internal filesystem based cache 6 | handler. 7 | 8 | See [Custom Cache Implementation](#caching.custom) for pointers on 9 | setting up your own cache handler. 10 | -------------------------------------------------------------------------------- /src/Compiler/BaseCompiler.php: -------------------------------------------------------------------------------- 1 | smarty; 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /src/Filter/FilterPluginWrapper.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 11 | } 12 | public function filter($code, \Smarty\Template $template) { 13 | return call_user_func($this->callback, $code, $template); 14 | } 15 | } -------------------------------------------------------------------------------- /src/UndefinedVariable.php: -------------------------------------------------------------------------------- 1 | display('index.tpl')` and 6 | `$smarty->display('file:index.tpl')` are identical in meaning. See the 7 | [resource](#resources) chapter for more details. 8 | -------------------------------------------------------------------------------- /src/Compile/Modifier/JsonEncodeModifierCompiler.php: -------------------------------------------------------------------------------- 1 | configLoad('test.conf')` 6 | and `$smarty->configLoad('file:test.conf')` are identical in meaning. 7 | See the [resource](#resources) chapter for more details. 8 | -------------------------------------------------------------------------------- /src/BlockHandler/Base.php: -------------------------------------------------------------------------------- 1 | cacheable; 18 | } 19 | } -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-unregister-filter.md: -------------------------------------------------------------------------------- 1 | unregisterFilter() 2 | 3 | dynamically unregister a filter 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | unregisterFilter 11 | 12 | string 13 | 14 | type 15 | 16 | string\|array 17 | 18 | callback 19 | 20 | Use this to dynamically unregister filters. It uses the following 21 | parameters: 22 | 23 | See also [`registerFilter()`](#api.register.filter). 24 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-smarty-debug-id.md: -------------------------------------------------------------------------------- 1 | \$smarty\_debug\_id {#variable.smarty.debug.id} 2 | =================== 3 | 4 | The value of `$smarty_debug_id` defines the URL keyword to enable 5 | debugging at browser level. The default value is `SMARTY_DEBUG`. 6 | 7 | See also [debugging console](#chapter.debugging.console) section, 8 | [`$debugging`](#variable.debugging) and 9 | [`$debugging_ctrl`](#variable.debugging.ctrl). 10 | -------------------------------------------------------------------------------- /src/Compile/Modifier/SubstrModifierCompiler.php: -------------------------------------------------------------------------------- 1 | cacheable; 16 | } 17 | 18 | public function handle($params, Template $template) { 19 | // TODO: Implement handle() method. 20 | } 21 | } -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-config-booleanize.md: -------------------------------------------------------------------------------- 1 | \$config\_booleanize {#variable.config.booleanize} 2 | ==================== 3 | 4 | If set to TRUE, [config files](#config.files) values of `on/true/yes` 5 | and `off/false/no` get converted to boolean values automatically. This 6 | way you can use the values in the template like so: 7 | `{if #foobar#}...{/if}`. If foobar was `on`, `true` or `yes`, the `{if}` 8 | statement will execute. Defaults to TRUE. 9 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-in_array.md: -------------------------------------------------------------------------------- 1 | # in_array 2 | 3 | test if value is contained in an array 4 | 5 | ## Basic usage 6 | 7 | ```smarty 8 | {if in_array('value2', $myarray)} value2 is in myarray{/if} 9 | ``` 10 | 11 | Can be replaced by operator *is in* 12 | 13 | ```smarty 14 | {if 'value2' is in $myarray}value2 is in myarray{/if} 15 | ``` 16 | 17 | See Also [operators](../language-basic-syntax/language-syntax-operators.md) 18 | -------------------------------------------------------------------------------- /src/Compile/Modifier/BCPluginWrapper.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 11 | } 12 | 13 | /** 14 | * @inheritDoc 15 | */ 16 | public function compile($params, \Smarty\Compiler\Template $compiler) { 17 | return call_user_func($this->callback, $params, $compiler); 18 | } 19 | } -------------------------------------------------------------------------------- /src/Compile/Modifier/StrlenModifierCompiler.php: -------------------------------------------------------------------------------- 1 | testInstall(); 20 | ?> 21 | 22 | 23 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-cache-locking.md: -------------------------------------------------------------------------------- 1 | \$cache\_locking {#variable.cache.locking} 2 | ================ 3 | 4 | Cache locking avoids concurrent cache generation. This means resource 5 | intensive pages can be generated only once, even if they\'ve been 6 | requested multiple times in the same moment. 7 | 8 | Cache locking is disabled by default. To enable it set `$cache_locking` 9 | to TRUE. 10 | 11 | See also [`$locking_timeout`](#variable.locking.timeout) 12 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-config-read-hidden.md: -------------------------------------------------------------------------------- 1 | \$config\_read\_hidden {#variable.config.read.hidden} 2 | ====================== 3 | 4 | If set to TRUE, hidden sections ie section names beginning with a 5 | period(.) in [config files](#config.files) can be read from templates. 6 | Typically you would leave this FALSE, that way you can store sensitive 7 | data in the config files such as database parameters and not worry about 8 | the template loading them. FALSE by default. 9 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-auto-literal.md: -------------------------------------------------------------------------------- 1 | \$auto\_literal {#variable.auto.literal} 2 | =============== 3 | 4 | The Smarty delimiter tags { and } will be ignored so long as they are 5 | surrounded by white space. This behavior can be disabled by setting 6 | auto\_literal to false. 7 | 8 | ::: {.informalexample} 9 | 10 | auto_literal = false; 12 | ?> 13 | 14 | 15 | ::: 16 | 17 | See also [Escaping Smarty parsing](#language.escaping), 18 | -------------------------------------------------------------------------------- /src/FunctionHandler/AttributeFunctionHandlerInterface.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | public function getSupportedAttributes(): array; 15 | } 16 | -------------------------------------------------------------------------------- /docs/api/variables/streams.md: -------------------------------------------------------------------------------- 1 | # Streams 2 | 3 | You can also use streams to call variables. *{$foo:bar}* will use the 4 | *foo://bar* stream to get the template variable. 5 | 6 | Using a PHP stream for a template variable resource from within a 7 | template. 8 | 9 | ```smarty 10 | {$foo:bar} 11 | ``` 12 | 13 | NB. Support for using streams to call variables is deprecated since Smarty v5.1 and will be removed 14 | in a future version. 15 | 16 | See also [`Template Resources`](../resources.md) 17 | -------------------------------------------------------------------------------- /src/Compile/Modifier/StrRepeatModifierCompiler.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 13 | $this->cacheable = $cacheable; 14 | } 15 | 16 | public function handle($params, Template $template) { 17 | $func = $this->callback; 18 | return $func($params, $template); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-unregister-resource.md: -------------------------------------------------------------------------------- 1 | unregisterResource() 2 | 3 | dynamically unregister a resource plugin 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | unregisterResource 11 | 12 | string 13 | 14 | name 15 | 16 | Pass in the `name` of the resource. 17 | 18 | 19 | unregisterResource('db'); 22 | 23 | ?> 24 | 25 | 26 | 27 | See also [`registerResource()`](#api.register.resource) and [template 28 | resources](#resources) 29 | -------------------------------------------------------------------------------- /src/Compile/Modifier/ModifierCompilerInterface.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 13 | $this->cacheable = $cacheable; 14 | } 15 | 16 | public function handle($params, $content, Template $template, &$repeat) { 17 | return \call_user_func_array($this->callback, [$params, $content, &$template, &$repeat]); 18 | } 19 | } -------------------------------------------------------------------------------- /src/Compile/Modifier/EmptyModifierCompiler.php: -------------------------------------------------------------------------------- 1 | setRawOutput(true); 19 | return ($params[0]); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-cache-id.md: -------------------------------------------------------------------------------- 1 | \$cache\_id {#variable.cache.id} 2 | =========== 3 | 4 | Persistent cache\_id identifier. As an alternative to passing the same 5 | `$cache_id` to each and every function call, you can set this 6 | `$cache_id` and it will be used implicitly thereafter. 7 | 8 | With a `$cache_id` you can have multiple cache files for a single call 9 | to [`display()`](#api.display) or [`fetch()`](#api.fetch) depending for 10 | example from different content of the same template. See the [caching 11 | section](#caching) for more information. 12 | -------------------------------------------------------------------------------- /src/Compile/Modifier/LowerModifierCompiler.php: -------------------------------------------------------------------------------- 1 | 10 | * @author Uwe Tews 11 | */ 12 | 13 | class LowerModifierCompiler extends Base { 14 | 15 | public function compile($params, \Smarty\Compiler\Template $compiler) { 16 | return 'mb_strtolower((string) ' . $params[ 0 ] . ', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')'; 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-unregister-cacheresource.md: -------------------------------------------------------------------------------- 1 | unregisterCacheResource() 2 | 3 | dynamically unregister a CacheResource plugin 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | unregisterCacheResource 11 | 12 | string 13 | 14 | name 15 | 16 | Pass in the `name` of the CacheResource. 17 | 18 | 19 | unregisterCacheResource('mysql'); 22 | 23 | ?> 24 | 25 | 26 | 27 | See also [`registerCacheResource()`](#api.register.cacheresource) and 28 | the [Custom CacheResource Implementation](#caching.custom) section. 29 | -------------------------------------------------------------------------------- /src/Compile/Modifier/CountParagraphsModifierCompiler.php: -------------------------------------------------------------------------------- 1 | **Note** 7 | > 8 | > Be sure any variables used within a non-cached section are also 9 | > assigned from PHP when the page is loaded from the cache. 10 | 11 | ```smarty 12 | Today's date is 13 | {nocache} 14 | {$smarty.now|date_format} 15 | {/nocache} 16 | ``` 17 | 18 | The above code will output the current date on a cached page. 19 | 20 | See also the [caching section](../../api/caching/basics.md). 21 | -------------------------------------------------------------------------------- /src/Compile/Modifier/IndentModifierCompiler.php: -------------------------------------------------------------------------------- 1 | ]*?>!', ' ', (string) {$params[0]})"; 17 | } else { 18 | return 'strip_tags((string) ' . $params[ 0 ] . ')'; 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /run-tests-for-all-php-versions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Runs tests for all supported PHP versions 4 | # Usage examples: 5 | # - ./run-tests-for-all-php-versions.sh --group 20221124 6 | # - ./run-tests-for-all-php-versions.sh --exclude-group slow 7 | 8 | COMPOSE_CMD="mutagen-compose" 9 | 10 | $COMPOSE_CMD run --rm php72 ./run-tests.sh $@ && \ 11 | $COMPOSE_CMD run --rm php73 ./run-tests.sh $@ && \ 12 | $COMPOSE_CMD run --rm php74 ./run-tests.sh $@ && \ 13 | $COMPOSE_CMD run --rm php80 ./run-tests.sh $@ && \ 14 | $COMPOSE_CMD run --rm php81 ./run-tests.sh $@ && \ 15 | $COMPOSE_CMD run --rm php82 ./run-tests.sh $@ 16 | $COMPOSE_CMD run --rm php83 ./run-tests.sh $@ 17 | $COMPOSE_CMD run --rm php84 ./run-tests.sh $@ 18 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-get-plugins-dir.md: -------------------------------------------------------------------------------- 1 | getPluginsDir() 2 | 3 | return the directory where plugins are stored 4 | 5 | Description 6 | =========== 7 | 8 | array 9 | 10 | getPluginsDir 11 | 12 | 13 | setPluginsDir(array( 17 | './plugins', 18 | './plugins_2', 19 | )); 20 | 21 | // get all directories where plugins are stored 22 | $config_dir = $smarty->getPluginsDir(); 23 | var_dump($config_dir); // array 24 | 25 | ?> 26 | 27 | 28 | 29 | See also [`setPluginsDir()`](#api.set.plugins.dir), 30 | [`addPluginsDir()`](#api.add.plugins.dir) and 31 | [`$plugins_dir`](#variable.plugins.dir). 32 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-unregister-plugin.md: -------------------------------------------------------------------------------- 1 | unregisterPlugin 2 | 3 | dynamically unregister plugins 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | unregisterPlugin 11 | 12 | string 13 | 14 | type 15 | 16 | string 17 | 18 | name 19 | 20 | This method unregisters plugins which previously have been registered by 21 | [registerPlugin()](#api.register.plugin), It uses the following 22 | parameters: 23 | 24 | 25 | 26 | 27 | unregisterPlugin("function","date_now"); 31 | 32 | ?> 33 | 34 | 35 | 36 | See also [`registerPlugin()`](#api.register.plugin). 37 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-clear-assign.md: -------------------------------------------------------------------------------- 1 | clearAssign() 2 | 3 | clears the value of an assigned variable 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | clearAssign 11 | 12 | mixed 13 | 14 | var 15 | 16 | This can be a single value, or an array of values. 17 | 18 | 19 | clearAssign('Name'); 22 | 23 | // clears multiple variables 24 | $smarty->clearAssign(array('Name', 'Address', 'Zip')); 25 | ?> 26 | 27 | 28 | 29 | See also [`clearAllAssign()`](#api.clear.all.assign), 30 | [`clearConfig()`](#api.clear.config), 31 | [`getTemplateVars()`](#api.get.template.vars), [`assign()`](#api.assign) 32 | and [`append()`](#api.append) 33 | -------------------------------------------------------------------------------- /src/Compile/Modifier/IssetModifierCompiler.php: -------------------------------------------------------------------------------- 1 | assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.'); 17 | ``` 18 | 19 | Where template is: 20 | 21 | ```smarty 22 | {$articleTitle} 23 | {$articleTitle|lower} 24 | ``` 25 | 26 | This will output: 27 | 28 | ``` 29 | Two Convicts Evade Noose, Jury Hung. 30 | two convicts evade noose, jury hung. 31 | ``` 32 | 33 | See also [`upper`](language-modifier-upper.md) and 34 | [`capitalize`](language-modifier-capitalize.md). 35 | -------------------------------------------------------------------------------- /src/Compile/Modifier/WordWrapModifierCompiler.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 24 | $this->modifierName = $modifierName; 25 | } 26 | 27 | public function handle(...$params) { 28 | try { 29 | return ($this->callback)(...$params); 30 | } catch (\ArgumentCountError $e) { 31 | throw new Exception("Invalid number of arguments to modifier " . $this->modifierName); 32 | } 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-count-words.md: -------------------------------------------------------------------------------- 1 | # count_words 2 | 3 | This is used to count the number of words in a variable. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {$myVar|count_words} 8 | ``` 9 | 10 | ## Examples 11 | 12 | ```php 13 | assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.'); 16 | 17 | ``` 18 | 19 | Where template is: 20 | 21 | ```smarty 22 | {$articleTitle} 23 | {$articleTitle|count_words} 24 | ``` 25 | 26 | This will output: 27 | 28 | ``` 29 | Dealers Will Hear Car Talk at Noon. 30 | 7 31 | ``` 32 | 33 | See also [`count_characters`](language-modifier-count-characters.md), 34 | [`count_paragraphs`](language-modifier-count-paragraphs.md) and 35 | [`count_sentences`](language-modifier-count-sentences.md). 36 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-upper.md: -------------------------------------------------------------------------------- 1 | # upper 2 | 3 | This is used to uppercase a variable. This is equivalent to the PHP 4 | [`strtoupper()`](https://www.php.net/strtoupper) function. 5 | 6 | ## Basic usage 7 | ```smarty 8 | {$myVar|upper} 9 | ``` 10 | 11 | ## Examples 12 | 13 | ```php 14 | assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While."); 16 | ``` 17 | 18 | Where template is: 19 | 20 | ```smarty 21 | {$articleTitle} 22 | {$articleTitle|upper} 23 | ``` 24 | 25 | Will output: 26 | 27 | ``` 28 | If Strike isn't Settled Quickly it may Last a While. 29 | IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. 30 | ``` 31 | 32 | See also [`lower`](./language-modifier-lower.md) and 33 | [`capitalize`](language-modifier-capitalize.md). 34 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-debugging.md: -------------------------------------------------------------------------------- 1 | \$debugging {#variable.debugging} 2 | =========== 3 | 4 | This enables the [debugging console](#chapter.debugging.console). The 5 | console is a javascript popup window that informs you of the 6 | [included](#language.function.include) templates, variables 7 | [assigned](#api.assign) from php and [config file 8 | variables](#language.config.variables) for the current script. It does 9 | not show variables assigned within a template with the 10 | [`{assign}`](#language.function.assign) function. 11 | 12 | The console can also be enabled from the url with 13 | [`$debugging_ctrl`](#variable.debugging.ctrl). 14 | 15 | See also [`{debug}`](#language.function.debug), 16 | [`$debug_tpl`](#variable.debug_template), and 17 | [`$debugging_ctrl`](#variable.debugging.ctrl). 18 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Smarty currently supports the latest minor version of Smarty 4 and Smarty 5. 6 | 7 | | Version | Supported | 8 | |---------|--------------------| 9 | | 5.0.x | :white_check_mark: | 10 | | 4.3.x | :white_check_mark: | 11 | | < 4.3 | :x: | 12 | 13 | ## Reporting a Vulnerability 14 | 15 | If you have discovered a security issue with Smarty, please contact us at mail [at] simonwisselink.nl. Do not 16 | disclose your findings publicly and **PLEASE** do not file an Issue (because that would disclose your findings 17 | publicly.) 18 | 19 | We will try to confirm the vulnerability and develop a fix if appropriate. When we release the fix, we will publish 20 | a security release. Please let us know if you want to be credited. 21 | -------------------------------------------------------------------------------- /src/Compile/Tag/ElseTag.php: -------------------------------------------------------------------------------- 1 | tag_nocache] = $this->closeTag($compiler, ['if', 'elseif']); 26 | $this->openTag($compiler, 'else', [$nesting, $compiler->tag_nocache]); 27 | return ''; 28 | } 29 | } -------------------------------------------------------------------------------- /src/Compile/DefaultHandlerBlockCompiler.php: -------------------------------------------------------------------------------- 1 | getSmarty()->getRuntime('DefaultPluginHandler')->hasPlugin(" . 11 | var_export($function, true) . ", 'block')"; 12 | } 13 | 14 | /** 15 | * @inheritDoc 16 | */ 17 | protected function getFullCallbackCode($tag, $function): string { 18 | return "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" . 19 | var_export($function, true) . ", 'block')"; 20 | } 21 | 22 | /** 23 | * @inheritDoc 24 | */ 25 | protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool { 26 | return true; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/Compile/Tag/SectionElse.php: -------------------------------------------------------------------------------- 1 | closeTag($compiler, ['section']); 26 | $this->openTag($compiler, 'sectionelse', ['sectionelse', $nocache_pushed]); 27 | return ""; 28 | } 29 | } -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-clear-all-assign.md: -------------------------------------------------------------------------------- 1 | clearAllAssign() 2 | 3 | clears the values of all assigned variables 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | clearAllAssign 11 | 12 | 13 | assign('Name', 'Fred'); 16 | $smarty->assign('Address', $address); 17 | 18 | // will output above 19 | print_r( $smarty->getTemplateVars() ); 20 | 21 | // clear all assigned variables 22 | $smarty->clearAllAssign(); 23 | 24 | // will output nothing 25 | print_r( $smarty->getTemplateVars() ); 26 | 27 | ?> 28 | 29 | 30 | 31 | See also [`clearAssign()`](#api.clear.assign), 32 | [`clearConfig()`](#api.clear.config), 33 | [`getTemplateVars()`](#api.get.template.vars), [`assign()`](#api.assign) 34 | and [`append()`](#api.append) 35 | -------------------------------------------------------------------------------- /src/Compile/Tag/BCPluginWrapper.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 21 | $this->cacheable = $cacheable; 22 | } 23 | 24 | /** 25 | * @inheritDoc 26 | */ 27 | public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string 28 | { 29 | return call_user_func($this->callback, $this->getAttributes($compiler, $args), $compiler->getSmarty()); 30 | } 31 | } -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-while.md: -------------------------------------------------------------------------------- 1 | # {while} 2 | 3 | `{while}` loops in Smarty have much the same flexibility as PHP 4 | [while](https://www.php.net/while) statements, with a few added features for 5 | the template engine. Every `{while}` must be paired with a matching 6 | `{/while}`. All [operators](../language-basic-syntax/language-syntax-operators.md) are recognized, such as *==*, 7 | *\|\|*, *or*, *&&*, *and*, etc and you can use modifiers as functions, such as *is_array()*. 8 | 9 | ## Examples 10 | ```smarty 11 | {while $foo > 0} 12 | {$foo--} 13 | {/while} 14 | ``` 15 | 16 | The above example will count down the value of $foo until 1 is reached. 17 | 18 | See also [`{foreach}`](./language-function-foreach.md), 19 | [`{for}`](./language-function-for.md) and 20 | [`{section}`](./language-function-section.md). 21 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-count.md: -------------------------------------------------------------------------------- 1 | # count 2 | 3 | Returns the number of elements in an array (or Countable object). Will return 0 for null. 4 | Returns 1 for any other type (such as a string). 5 | 6 | If the optional mode parameter is set to 1, count() will recursively count the array. 7 | This is particularly useful for counting all the elements of a multidimensional array. 8 | 9 | ## Basic usage 10 | ```smarty 11 | {if $myVar|count > 3}4 or more{/if} 12 | {if count($myVar) > 3}4 or more{/if} 13 | ``` 14 | 15 | 16 | ## Parameters 17 | 18 | | Parameter | Type | Required | Description | 19 | |-----------|------|----------|--------------------------------------------------------| 20 | | 1 | int | No | If set to 1, count() will recursively count the array. | 21 | 22 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-join.md: -------------------------------------------------------------------------------- 1 | # join 2 | 3 | Returns a string containing all the element of the given array 4 | with the separator string between each. 5 | 6 | ## Basic usage 7 | 8 | For `$myArray` populated with `['a','b','c']`, the following will return the string `abc`. 9 | ```smarty 10 | {$myArray|join} 11 | ``` 12 | 13 | 14 | ## Parameters 15 | 16 | | Parameter | Type | Required | Description | 17 | |-----------|--------|----------|-------------------------------------------------------------| 18 | | 1 | string | No | glue used between array elements. Defaults to empty string. | 19 | 20 | ## Examples 21 | 22 | 23 | For `$myArray` populated with `[1,2,3]`, the following will return the string `1-2-3`. 24 | ```smarty 25 | {$myArray|join:"-"} 26 | ``` -------------------------------------------------------------------------------- /src/Template/Config.php: -------------------------------------------------------------------------------- 1 | true]; 28 | 29 | public function createCompiler(): \Smarty\Compiler\BaseCompiler { 30 | return new \Smarty\Compiler\Configfile($this->smarty); 31 | } 32 | 33 | protected static function getDefaultHandlerFunc(Smarty $smarty) { 34 | return $smarty->default_config_handler_func; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-cat.md: -------------------------------------------------------------------------------- 1 | # cat 2 | 3 | This value is concatenated to the given variable. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {$myVar|cat:' units'} 8 | ``` 9 | 10 | ## Parameters 11 | 12 | | Parameter | Type | Required | Description | 13 | |-----------|--------|----------|--------------------------------------------------| 14 | | 1 | string | No | This value to concatenate to the given variable. | 15 | 16 | ## Examples 17 | 18 | ```php 19 | assign('articleTitle', "Psychics predict world didn't end"); 22 | 23 | ``` 24 | 25 | Where template is: 26 | 27 | ```smarty 28 | {$articleTitle|cat:' yesterday.'} 29 | ``` 30 | 31 | Will output: 32 | 33 | ``` 34 | Psychics predict world didn't end yesterday. 35 | ``` 36 | 37 | -------------------------------------------------------------------------------- /src/Compile/Tag/ForElse.php: -------------------------------------------------------------------------------- 1 | closeTag($compiler, ['for']); 27 | $this->openTag($compiler, 'forelse', ['forelse', $nocache_pushed]); 28 | return ""; 29 | } 30 | } -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-clear-config.md: -------------------------------------------------------------------------------- 1 | clearConfig() 2 | 3 | clears assigned config variables 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | clearConfig 11 | 12 | string 13 | 14 | var 15 | 16 | This clears all assigned [config variables](#language.config.variables). 17 | If a variable name is supplied, only that variable is cleared. 18 | 19 | 20 | clearConfig(); 23 | 24 | // clear one variable 25 | $smarty->clearConfig('foobar'); 26 | ?> 27 | 28 | 29 | 30 | See also [`getConfigVars()`](#api.get.config.vars), 31 | [`config variables`](#language.config.variables), 32 | [`config files`](#config.files), 33 | [`{config_load}`](#language.function.config.load), 34 | [`configLoad()`](#api.config.load) and 35 | [`clearAssign()`](#api.clear.assign). 36 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-mute-expected-errors.md: -------------------------------------------------------------------------------- 1 | Smarty::muteExpectedErrors() 2 | 3 | mutes expected warnings and notices deliberately generated by Smarty 4 | 5 | Description 6 | =========== 7 | 8 | string 9 | 10 | muteExpectedErrors 11 | 12 | muteExpectedErrors() registers a custom error handler using 13 | [set\_error\_handler()](https://www.php.net/set_error_handler). The error 14 | handler merely inspects `$errno` and `$errfile` to determine if the 15 | given error was produced deliberately and must be ignored, or should be 16 | passed on to the next error handler. 17 | 18 | `\Smarty\Smarty::unmuteExpectedErrors()` removes the current error handler. 19 | Please note, that if you\'ve registered any custom error handlers after 20 | the muteExpectedErrors() call, the unmute will not remove Smarty\'s 21 | muting error handler, but the one registered last. 22 | -------------------------------------------------------------------------------- /docs/designers/language-custom-functions/language-function-debug.md: -------------------------------------------------------------------------------- 1 | # {debug} 2 | 3 | `{debug}` dumps the debug console to the page. This works regardless of 4 | the [debug](../chapter-debugging-console.md) settings in the php script. 5 | Since this gets executed at runtime, this is only able to show the 6 | [assigned](../../programmers/api-functions/api-assign.md) variables; not the templates that are in use. 7 | However, you can see all the currently available variables within the 8 | scope of a template. 9 | 10 | | Attribute Name | Required | Description | 11 | |----------------|----------|------------------------------------------------------------| 12 | | output | No | output type, html or javascript (defaults to 'javascript') | 13 | 14 | See also the [debugging console page](../chapter-debugging-console.md). 15 | -------------------------------------------------------------------------------- /src/ParseTree/Base.php: -------------------------------------------------------------------------------- 1 | **Note** 10 | > 11 | > It is not recommended to put this directory under the web server 12 | > document root. 13 | 14 | > **Note** 15 | > 16 | > As of Smarty 3.1 the attribute \$config\_dir is no longer accessible 17 | > directly. Use [`getConfigDir()`](#api.get.config.dir), 18 | > [`setConfigDir()`](#api.set.config.dir) and 19 | > [`addConfigDir()`](#api.add.config.dir) instead. 20 | 21 | See also [`getConfigDir()`](#api.get.config.dir), 22 | [`setConfigDir()`](#api.set.config.dir) and 23 | [`addConfigDir()`](#api.add.config.dir). 24 | -------------------------------------------------------------------------------- /src/Compile/Tag/Nocache.php: -------------------------------------------------------------------------------- 1 | openTag($compiler, 'nocache'); 34 | return ''; 35 | } 36 | } -------------------------------------------------------------------------------- /docs/designers/language-custom-functions/index.md: -------------------------------------------------------------------------------- 1 | # Custom Tags 2 | 3 | Smarty comes with several custom plugin functions that you can use in 4 | the templates. 5 | 6 | - [{counter}](language-function-counter.md) 7 | - [{cycle}](language-function-cycle.md) 8 | - [{eval}](language-function-eval.md) 9 | - [{fetch}](language-function-fetch.md) 10 | - [{html_checkboxes}](language-function-html-checkboxes.md) 11 | - [{html_image}](language-function-html-image.md) 12 | - [{html_options}](language-function-html-options.md) 13 | - [{html_radios}](language-function-html-radios.md) 14 | - [{html_select_date}](language-function-html-select-date.md) 15 | - [{html_select_time}](language-function-html-select-time.md) 16 | - [{html_table}](language-function-html-table.md) 17 | - [{mailto}](language-function-mailto.md) 18 | - [{math}](language-function-math.md) 19 | - [{textformat}](language-function-textformat.md) 20 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-nl2br.md: -------------------------------------------------------------------------------- 1 | # nl2br 2 | 3 | All `"\n"` line breaks will be converted to html `
` tags in the 4 | given variable. This is equivalent to the PHP\'s 5 | [`nl2br()`](https://www.php.net/nl2br) function. 6 | 7 | ## Basic usage 8 | ```smarty 9 | {$myVar|nl2br} 10 | ``` 11 | 12 | ## Examples 13 | 14 | ```php 15 | assign('articleTitle', 18 | "Sun or rain expected\ntoday, dark tonight" 19 | ); 20 | 21 | ``` 22 | 23 | Where the template is: 24 | 25 | ```smarty 26 | {$articleTitle|nl2br} 27 | ``` 28 | 29 | Will output: 30 | 31 | ``` 32 | Sun or rain expected
today, dark tonight 33 | ``` 34 | 35 | See also [`word_wrap`](language-modifier-wordwrap.md), 36 | [`count_paragraphs`](language-modifier-count-paragraphs.md) and 37 | [`count_sentences`](language-modifier-count-sentences.md). 38 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-get-config-vars.md: -------------------------------------------------------------------------------- 1 | getConfigVars() 2 | 3 | returns the given loaded config variable value 4 | 5 | Description 6 | =========== 7 | 8 | array 9 | 10 | getConfigVars 11 | 12 | string 13 | 14 | varname 15 | 16 | If no parameter is given, an array of all loaded [config 17 | variables](#language.config.variables) is returned. 18 | 19 | 20 | getConfigVars('foo'); 24 | 25 | // get all loaded config template vars 26 | $all_config_vars = $smarty->getConfigVars(); 27 | 28 | // take a look at them 29 | print_r($all_config_vars); 30 | ?> 31 | 32 | 33 | 34 | See also [`clearConfig()`](#api.clear.config), 35 | [`{config_load}`](#language.function.config.load), 36 | [`configLoad()`](#api.config.load) and 37 | [`getTemplateVars()`](#api.get.template.vars). 38 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-debug.md: -------------------------------------------------------------------------------- 1 | # {debug} 2 | 3 | `{debug}` dumps the debug console to the page. This works regardless of 4 | the [debug](../chapter-debugging-console.md) settings in the php script. 5 | Since this gets executed at runtime, this is only able to show the 6 | [assigned](../../programmers/api-functions/api-assign.md) variables; not the templates that are in use. 7 | However, you can see all the currently available variables within the 8 | scope of a template. 9 | 10 | If caching is enabled and a page is loaded from cache `{debug}` does 11 | show only the variables which assigned for the cached page. 12 | 13 | In order to see also the variables which have been locally assigned 14 | within the template it does make sense to place the `{debug}` tag at the 15 | end of the template. 16 | 17 | See also the [debugging console page](../chapter-debugging-console.md). 18 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-debugging-ctrl.md: -------------------------------------------------------------------------------- 1 | \$debugging\_ctrl {#variable.debugging.ctrl} 2 | ================= 3 | 4 | This allows alternate ways to enable debugging. `NONE` means no 5 | alternate methods are allowed. `URL` means when the keyword 6 | `SMARTY_DEBUG` is found in the `QUERY_STRING`, debugging is enabled for 7 | that invocation of the script. If [`$debugging`](#variable.debugging) is 8 | TRUE, this value is ignored. 9 | 10 | 11 | debugging = false; // the default 15 | $smarty->debugging_ctrl = ($_SERVER['SERVER_NAME'] == 'localhost') ? 'URL' : 'NONE'; 16 | ?> 17 | 18 | See also [debugging console](#chapter.debugging.console) section, 19 | [`$debugging`](#variable.debugging) and 20 | [`$smarty_debug_id`](#variable.smarty.debug.id). 21 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-error-reporting.md: -------------------------------------------------------------------------------- 1 | \$error\_reporting {#variable.error.reporting} 2 | ================== 3 | 4 | When this value is set to a non-null-value it\'s value is used as php\'s 5 | [`error_reporting`](https://www.php.net/error_reporting) level inside of 6 | [`display()`](#api.display) and [`fetch()`](#api.fetch). 7 | 8 | Smarty 3.1.2 introduced the 9 | [`muteExpectedErrors()`](#api.mute.expected.errors) function. Calling 10 | `\Smarty\Smarty::muteExpectedErrors();` after setting up custom error handling 11 | will ensure that warnings and notices (deliberately) produced by Smarty 12 | will not be passed to other custom error handlers. If your error logs 13 | are filling up with warnings regarding `filemtime()` or `unlink()` 14 | calls, please enable Smarty\'s error muting. 15 | 16 | See also [debugging](#chapter.debugging.console) and 17 | [troubleshooting](#troubleshooting). 18 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-get-template-vars.md: -------------------------------------------------------------------------------- 1 | getTemplateVars() 2 | 3 | returns assigned variable value(s) 4 | 5 | Description 6 | =========== 7 | 8 | array 9 | 10 | getTemplateVars 11 | 12 | string 13 | 14 | varname 15 | 16 | If no parameter is given, an array of all [assigned](#api.assign) 17 | variables are returned. 18 | 19 | 20 | getTemplateVars('foo'); 23 | 24 | // get all assigned template vars 25 | $all_tpl_vars = $smarty->getTemplateVars(); 26 | 27 | // take a look at them 28 | print_r($all_tpl_vars); 29 | ?> 30 | 31 | 32 | 33 | See also [`assign()`](#api.assign), 34 | [`{assign}`](#language.function.assign), [`append()`](#api.append), 35 | [`clearAssign()`](#api.clear.assign), 36 | [`clearAllAssign()`](#api.clear.all.assign) and 37 | [`getConfigVars()`](#api.get.config.vars) 38 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-escape-html.md: -------------------------------------------------------------------------------- 1 | \$escape\_html {#variable.escape.html} 2 | ============== 3 | 4 | Setting `$escape_html` to TRUE will escape all template variable output 5 | by wrapping it in 6 | `htmlspecialchars({$output}, ENT_QUOTES, $char_set);`, 7 | which is the same as `{$variable|escape:"html"}`. 8 | 9 | Template designers can choose to selectively disable this feature by 10 | adding the `nofilter` flag: `{$variable nofilter}`. 11 | 12 | Modifiers and Filters are run in the following order: modifier, 13 | default\_modifier, \$escape\_html, registered variable filters, 14 | autoloaded variable filters, template instance\'s variable filters. 15 | Everything except the individual modifier can be disabled with the 16 | `nofilter` flag. 17 | 18 | > **Note** 19 | > 20 | > This is a compile time option. If you change the setting you must make 21 | > sure that the templates get recompiled. 22 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-substr.md: -------------------------------------------------------------------------------- 1 | # substr 2 | 3 | Returns a part (substring) of the given string starting at a given offset. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {"Smarty"|substr:2} # renders: arty 8 | {"Smarty"|substr:2:3} # renders: art 9 | ``` 10 | 11 | ## Parameters 12 | 13 | | Parameter | Type | Required | Description | 14 | |-----------|------|----------|-----------------------------------------------------| 15 | | 1 | int | yes | offset (zero based, can be negative) | 16 | | 2 | int | no | length of substring returned (unlimited of omitted) | 17 | 18 | 19 | ## Examples 20 | 21 | When used with a negative offset, the substring starts n characters from the end of the string counting backwards. 22 | ```smarty 23 | {"Smarty"|substr:-2} # renders: ty 24 | {"Smarty"|substr:-2:1} # renders: t 25 | ``` -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-load-filter.md: -------------------------------------------------------------------------------- 1 | loadFilter() 2 | 3 | load a filter plugin 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | loadFilter 11 | 12 | string 13 | 14 | type 15 | 16 | string 17 | 18 | name 19 | 20 | The first argument specifies the type of the filter to load and can be 21 | one of the following: `variable`, `pre`, `post` or `output`. The second argument 22 | specifies the `name` of the filter plugin. 23 | 24 | 25 | loadFilter('pre', 'trim'); 29 | 30 | // load another prefilter named 'datefooter' 31 | $smarty->loadFilter('pre', 'datefooter'); 32 | 33 | // load output filter named 'compress' 34 | $smarty->loadFilter('output', 'compress'); 35 | 36 | ?> 37 | 38 | 39 | 40 | See also [`registerFilter()`](#api.register.filter) and [advanced 41 | features](#advanced.features). 42 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-clear-all-cache.md: -------------------------------------------------------------------------------- 1 | clearAllCache() 2 | 3 | clears the entire template cache 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | clearAllCache 11 | 12 | int 13 | 14 | expire\_time 15 | 16 | As an optional parameter, you can supply a minimum age in seconds the 17 | cache files must be before they will get cleared. 18 | 19 | > **Note** 20 | > 21 | > Since Smarty version 3.1.14 it is possible to delete cache files by 22 | > their individual expiration time at creation by passing constant 23 | > SMARTY::CLEAR\_EXPIRED as `expire_time` parameter. 24 | 25 | 26 | clearAllCache(); 29 | 30 | // clears all files over one hour old 31 | $smarty->clearAllCache(3600); 32 | ?> 33 | 34 | 35 | 36 | See also [`clearCache()`](#api.clear.cache), 37 | [`isCached()`](#api.is.cached) and the [caching](#caching) page. 38 | -------------------------------------------------------------------------------- /src/Compile/Tag/Rdelim.php: -------------------------------------------------------------------------------- 1 | getTemplate()->getRightDelimiter(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-enable-security.md: -------------------------------------------------------------------------------- 1 | enableSecurity() 2 | 3 | enables template security 4 | 5 | Description 6 | =========== 7 | 8 | string 9 | 10 | enableSecurity 11 | 12 | string 13 | 14 | securityclass 15 | 16 | string 17 | 18 | enableSecurity 19 | 20 | object 21 | 22 | securityobject 23 | 24 | string 25 | 26 | enableSecurity 27 | 28 | This enables security checking on templates. It uses the following 29 | parameters: 30 | 31 | - `securityclass` is an optional parameter. It\'s the name of the 32 | class with defines the security policy parameters. 33 | 34 | - `securityobject` is an optional parameter. It\'s the object with 35 | defines the security policy parameters. 36 | 37 | For the details how to setup a security policy see the 38 | [Security](#advanced.features.security) section. 39 | 40 | See also [`disableSecurity()`](#api.disable.security), and 41 | [Security](#advanced.features.security). 42 | -------------------------------------------------------------------------------- /src/Compile/Tag/NocacheClose.php: -------------------------------------------------------------------------------- 1 | closeTag($compiler, ['nocache']); 35 | return ''; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-get-config-dir.md: -------------------------------------------------------------------------------- 1 | getConfigDir() 2 | 3 | return the directory where config files are stored 4 | 5 | Description 6 | =========== 7 | 8 | string\|array 9 | 10 | getConfigDir 11 | 12 | string 13 | 14 | key 15 | 16 | 17 | setConfigDir(array( 21 | 'one' => './config', 22 | 'two' => './config_2', 23 | 'three' => './config_3', 24 | )); 25 | 26 | // get all directories where config files are stored 27 | $config_dir = $smarty->getConfigDir(); 28 | var_dump($config_dir); // array 29 | 30 | // get directory identified by key 31 | $config_dir = $smarty->getConfigDir('one'); 32 | var_dump($config_dir); // string 33 | 34 | ?> 35 | 36 | 37 | 38 | See also [`setConfigDir()`](#api.set.config.dir), 39 | [`addConfigDir()`](#api.add.config.dir) and 40 | [`$config_dir`](#variable.config.dir). 41 | -------------------------------------------------------------------------------- /src/Resource/RecompiledPlugin.php: -------------------------------------------------------------------------------- 1 | /U",'',$tpl_source); 21 | } 22 | 23 | // register the prefilter 24 | $smarty->registerFilter('pre', 'remove_dw_comments'); 25 | $smarty->display('index.tpl'); 26 | ``` 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Smarty template engine 2 | Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. 3 | 4 | ![CI](https://github.com/smarty-php/smarty/workflows/CI/badge.svg) 5 | 6 | ## Documentation 7 | Read the [documentation](https://smarty-php.github.io/smarty/) to find out how to use it. 8 | 9 | ## Requirements 10 | Smarty v5 can be run with PHP 7.2 to PHP 8.5. 11 | 12 | ## Installation 13 | Smarty versions 3.1.11 or later can be installed with [Composer](https://getcomposer.org/). 14 | 15 | To get the latest stable version of Smarty use: 16 | ```bash 17 | composer require smarty/smarty 18 | ```` 19 | 20 | More in the [Getting Started](./docs/getting-started.md) section of the docs. 21 | 22 | ## Sponsors 23 | 24 | Smarty is sponsored by: 25 | - Marc Laporte [@marclaporte](https://github.com/marclaporte) 26 | - [Temma](https://github.com/Digicreon/Temma), the MVC framework based on Smarty 27 | 28 | Thank you! -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-merge-compiled-includes.md: -------------------------------------------------------------------------------- 1 | \$merge\_compiled\_includes {#variable.merge.compiled.includes} 2 | =========================== 3 | 4 | By setting `$merge_compiled_includes` to TRUE Smarty will merge the 5 | compiled template code of subtemplates into the compiled code of the 6 | main template. This increases rendering speed of templates using a many 7 | different sub-templates. 8 | 9 | Individual sub-templates can be merged by setting the `inline` option 10 | flag within the `{include}` tag. `$merge_compiled_includes` does not 11 | have to be enabled for the `inline` merge. 12 | 13 | ::: {.informalexample} 14 | 15 | merge_compiled_includes = true; 17 | ?> 18 | 19 | 20 | ::: 21 | 22 | > **Note** 23 | > 24 | > This is a compile time option. If you change the setting you must make 25 | > sure that the templates get recompiled. 26 | 27 | See also [`{include}`](#language.function.include) tag 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: lexers parsers 2 | 3 | lexers: src/Lexer/ConfigfileLexer.php src/Lexer/TemplateLexer.php 4 | parsers: src/Parser/ConfigfileParser.php src/Parser/TemplateParser.php 5 | 6 | src/Lexer/ConfigfileLexer.php: src/Lexer/ConfigfileLexer.plex 7 | php ./utilities/make-lexer.php src/Lexer/ConfigfileLexer.plex src/Lexer/ConfigfileLexer.php 8 | 9 | src/Lexer/TemplateLexer.php: src/Lexer/TemplateLexer.plex 10 | php ./utilities/make-lexer.php src/Lexer/TemplateLexer.plex src/Lexer/TemplateLexer.php 11 | 12 | src/Parser/ConfigfileParser.php: src/Parser/ConfigfileParser.y 13 | php ./utilities/make-parser.php src/Parser/ConfigfileParser.y src/Parser/ConfigfileParser.php 14 | 15 | src/Parser/TemplateParser.php: src/Parser/TemplateParser.y 16 | php ./utilities/make-parser.php src/Parser/TemplateParser.y src/Parser/TemplateParser.php 17 | 18 | clean: 19 | rm -f src/Lexer/ConfigfileLexer.php src/Lexer/TemplateLexer.php src/Parser/ConfigfileParser.php src/Parser/TemplateParser.php -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-get-registered-object.md: -------------------------------------------------------------------------------- 1 | getRegisteredObject() 2 | 3 | returns a reference to a registered object 4 | 5 | Description 6 | =========== 7 | 8 | array 9 | 10 | getRegisteredObject 11 | 12 | string 13 | 14 | object\_name 15 | 16 | This is useful from within a custom function when you need direct access 17 | to a [registered object](#api.register.object). See the 18 | [objects](#advanced.features.objects) page for more info. 19 | 20 | 21 | getRegisteredObject($params['object']); 27 | // use $obj_ref is now a reference to the object 28 | } 29 | } 30 | ?> 31 | 32 | 33 | 34 | See also [`registerObject()`](#api.register.object), 35 | [`unregisterObject()`](#api.unregister.object) and [objects 36 | page](#advanced.features.objects) 37 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-number-format.md: -------------------------------------------------------------------------------- 1 | # number_format 2 | 3 | Allows you to format a number using decimals and a thousands-separator. By default, the number of decimals is 0 4 | and the number is rounded. 5 | 6 | ## Basic usage 7 | ```smarty 8 | {$num = 2000.151} 9 | {$num|number_format} # renders: 2,000 10 | ``` 11 | 12 | 13 | ## Parameters 14 | 15 | | Parameter | Type | Required | Description | 16 | |-----------|--------|----------|---------------------------------------| 17 | | 1 | int | No | number of decimals (defaults to 0) | 18 | | 2 | string | No | decimal separator (defaults to ".") | 19 | | 3 | string | No | thousands-separator (defaults to ",") | 20 | 21 | 22 | ## Examples 23 | 24 | ```smarty 25 | {$num = 2000.151} 26 | {$num|number_format:2} # renders: 2,000.15 27 | ``` 28 | 29 | ```smarty 30 | {$num = 2000.151} 31 | {$num|number_format:2:".":""} # renders: 2000.15 32 | ``` -------------------------------------------------------------------------------- /src/ParseTree/Code.php: -------------------------------------------------------------------------------- 1 | data = $data; 32 | } 33 | 34 | /** 35 | * Return buffer content in parentheses 36 | * 37 | * @param \Smarty\Parser\TemplateParser $parser 38 | * 39 | * @return string content 40 | */ 41 | public function to_smarty_php(\Smarty\Parser\TemplateParser $parser) 42 | { 43 | return sprintf('(%s)', $this->data); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/api/filters/postfilters.md: -------------------------------------------------------------------------------- 1 | # Postfilters 2 | 3 | Template postfilters are PHP functions that your templates are ran 4 | through *after they are compiled*. 5 | 6 | Smarty will 7 | pass the compiled template code as the first argument, and expect the 8 | function to return the result of the processing, which must also be valid PHP code. 9 | 10 | Prefilters can be either added as part of an [Extension](../extending/extensions.md) or 11 | registered as shown below. 12 | 13 | 14 | ```php 15 | \n\"; ?>\n".$tpl_source; 20 | } 21 | 22 | // register the postfilter 23 | $smarty->registerFilter('post', 'add_header_comment'); 24 | $smarty->display('index.tpl'); 25 | ``` 26 | 27 | The postfilter above will make the compiled Smarty template `index.tpl` 28 | look like: 29 | 30 | ```smarty 31 | 32 | {* rest of template content... *} 33 | ``` 34 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-register-object.md: -------------------------------------------------------------------------------- 1 | registerObject() 2 | 3 | register an object for use in the templates 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | registerObject 11 | 12 | string 13 | 14 | object\_name 15 | 16 | object 17 | 18 | object 19 | 20 | array 21 | 22 | allowed\_methods\_properties 23 | 24 | boolean 25 | 26 | format 27 | 28 | array 29 | 30 | block\_methods 31 | 32 | > **Note** 33 | > 34 | > When you register/assign objects to templates, be sure that all 35 | > properties and methods accessed from the template are for presentation 36 | > purposes only. It is very easy to inject application logic through 37 | > objects, and this leads to poor designs that are difficult to manage. 38 | > See the Best Practices section of the Smarty website. 39 | 40 | See the [objects section](#advanced.features.objects) for more 41 | information. 42 | 43 | See also [`getRegisteredObject()`](#api.get.registered.object), and 44 | [`unregisterObject()`](#api.unregister.object). 45 | -------------------------------------------------------------------------------- /docs/designers/language-combining-modifiers.md: -------------------------------------------------------------------------------- 1 | # Combining Modifiers 2 | 3 | You can apply any number of modifiers to a variable. They will be 4 | applied in the order they are combined, from left to right. They must be 5 | separated with a `|` (pipe) character. 6 | 7 | ```php 8 | assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.'); 11 | ``` 12 | 13 | where template is: 14 | 15 | ```smarty 16 | {$articleTitle} 17 | {$articleTitle|upper|spacify} 18 | {$articleTitle|lower|spacify|truncate} 19 | {$articleTitle|lower|truncate:30|spacify} 20 | {$articleTitle|lower|spacify|truncate:30:". . ."} 21 | ``` 22 | 23 | 24 | The above example will output: 25 | 26 | ``` 27 | Smokers are Productive, but Death Cuts Efficiency. 28 | S M O K E R S A R ....snip.... H C U T S E F F I C I E N C Y . 29 | s m o k e r s a r ....snip.... b u t d e a t h c u t s... 30 | s m o k e r s a r e p r o d u c t i v e , b u t . . . 31 | s m o k e r s a r e p. . . 32 | ``` 33 | -------------------------------------------------------------------------------- /docs/api/extending/modifiers.md: -------------------------------------------------------------------------------- 1 | # Custom modifiers 2 | 3 | Modifiers are little functions that are applied 4 | to a variable in the template before it is displayed or used in some 5 | other context. Smarty comes with a bunch of [modifiers](../../designers/language-modifiers/index.md), but you can 6 | easily add your own. 7 | 8 | In order to do so, you must write a function that accepts as its first parameter the value on which the 9 | modifier is to operate. The rest of the parameters are optional, depending on what kind of operation is to be performed. 10 | 11 | The modifier has to return the result of its processing. 12 | 13 | For example: 14 | ```php 15 | registerPlugin(Smarty\Smarty::PLUGIN_MODIFIER, 'substr', 'smarty_modifier_substr'); 22 | ``` 23 | 24 | You can now use this in your templates as follows: 25 | ```smarty 26 | {$applicationName|substr:0:20} 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-string-format.md: -------------------------------------------------------------------------------- 1 | # string_format 2 | 3 | This is a way to format strings, such as decimal numbers and such. Use 4 | the syntax for [`sprintf()`](https://www.php.net/sprintf) for the 5 | formatting. 6 | 7 | ## Basic usage 8 | ```smarty 9 | {$myVar|string_format:"%d"} 10 | ``` 11 | 12 | ## Parameters 13 | 14 | | Parameter Position | Type | Required | Description | 15 | |--------------------|--------|----------|---------------------------------------| 16 | | 1 | string | Yes | This is what format to use. (sprintf) | 17 | 18 | ## Examples 19 | 20 | ```php 21 | assign('number', 23.5787446); 24 | 25 | ``` 26 | 27 | Where template is: 28 | 29 | ```smarty 30 | {$number} 31 | {$number|string_format:"%.2f"} 32 | {$number|string_format:"%d"} 33 | ``` 34 | 35 | Will output: 36 | 37 | ``` 38 | 23.5787446 39 | 23.58 40 | 23 41 | ``` 42 | 43 | See also [`date_format`](language-modifier-date-format.md). 44 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-add-plugins-dir.md: -------------------------------------------------------------------------------- 1 | addPluginsDir() 2 | 3 | add a directory to the list of directories where plugins are stored 4 | 5 | Description 6 | =========== 7 | 8 | Smarty 9 | 10 | addPluginsDir 11 | 12 | string\|array 13 | 14 | plugins\_dir 15 | 16 | 17 | addPluginsDir('./plugins_1'); 21 | 22 | // add multiple directories where plugins are stored 23 | $smarty->setPluginsDir(array( 24 | './plugins_2', 25 | './plugins_3', 26 | )); 27 | 28 | // view the plugins dir chain 29 | var_dump($smarty->getPluginsDir()); 30 | 31 | // chaining of method calls 32 | $smarty->setPluginsDir('./plugins') 33 | ->addPluginsDir('./plugins_1') 34 | ->addPluginsDir('./plugins_2'); 35 | 36 | ?> 37 | 38 | 39 | 40 | See also [`getPluginsDir()`](#api.get.plugins.dir), 41 | [`setPluginsDir()`](#api.set.plugins.dir) and 42 | [`$plugins_dir`](#variable.plugins.dir). 43 | -------------------------------------------------------------------------------- /src/ParseTree/DqContent.php: -------------------------------------------------------------------------------- 1 | data = $data; 31 | } 32 | 33 | /** 34 | * Return content as double-quoted string 35 | * 36 | * @param \Smarty\Parser\TemplateParser $parser 37 | * 38 | * @return string doubled quoted string 39 | */ 40 | public function to_smarty_php(\Smarty\Parser\TemplateParser $parser) 41 | { 42 | return '"' . $this->data . '"'; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /docs/api/variables/static-class-methods.md: -------------------------------------------------------------------------------- 1 | # Static Classes 2 | 3 | You can directly access static classes. The syntax is roughly the same as in 4 | PHP. 5 | 6 | > **Note** 7 | > 8 | > Direct access to PHP classes is not recommended. This ties the 9 | > underlying application code structure directly to the presentation, 10 | > and also complicates template syntax. It is recommended to register 11 | > plugins which insulate templates from PHP classes/objects. Use at your 12 | > own discretion. 13 | 14 | ## Examples 15 | 16 | **class constant BAR** 17 | ```smarty 18 | {assign var=foo value=myclass::BAR} 19 | ``` 20 | 21 | **method result** 22 | ```smarty 23 | {assign var=foo value=myclass::method()} 24 | ``` 25 | 26 | **method chaining** 27 | ```smarty 28 | {assign var=foo value=myclass::method1()->method2} 29 | ``` 30 | 31 | **property bar of class myclass** 32 | ```smarty 33 | {assign var=foo value=myclass::$bar} 34 | ``` 35 | 36 | **using Smarty variable bar as class name** 37 | ```smarty 38 | {assign var=foo value=$bar::method} 39 | ``` 40 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-count-sentences.md: -------------------------------------------------------------------------------- 1 | # count_sentences 2 | 3 | This is used to count the number of sentences in a variable. A sentence 4 | being delimited by a dot, question- or exclamation-mark (.?!). 5 | 6 | ## Basic usage 7 | ```smarty 8 | {$myVar|count_sentences} 9 | ``` 10 | 11 | ## Examples 12 | 13 | ```php 14 | assign('articleTitle', 17 | 'Two Soviet Ships Collide - One Dies. 18 | Enraged Cow Injures Farmer with Axe.' 19 | ); 20 | 21 | ``` 22 | 23 | Where template is: 24 | 25 | ```smarty 26 | {$articleTitle} 27 | {$articleTitle|count_sentences} 28 | ``` 29 | 30 | Will output: 31 | 32 | ``` 33 | Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe. 34 | 2 35 | ``` 36 | 37 | See also [`count_characters`](language-modifier-count-characters.md), 38 | [`count_paragraphs`](language-modifier-count-paragraphs.md) and 39 | [`count_words`](language-modifier-count-words.md). 40 | -------------------------------------------------------------------------------- /src/Extension/Base.php: -------------------------------------------------------------------------------- 1 | loopNesting--; 34 | 35 | $nocache_pushed = $this->closeTag($compiler, ['while']); 36 | 37 | if ($nocache_pushed) { 38 | // pop the pushed virtual nocache tag 39 | $this->closeTag($compiler, 'nocache'); 40 | $compiler->tag_nocache = true; 41 | } 42 | 43 | return "\n"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-register-cacheresource.md: -------------------------------------------------------------------------------- 1 | registerCacheResource() 2 | 3 | dynamically register CacheResources 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | registerCacheResource 11 | 12 | string 13 | 14 | name 15 | 16 | Smarty\_CacheResource 17 | 18 | resource\_handler 19 | 20 | Use this to dynamically register a [CacheResource 21 | plugin](#caching.custom) with Smarty. Pass in the `name` of the 22 | CacheResource and the object extending Smarty\_CacheResource. See 23 | [Custom Cache Implementation](#caching.custom) for more information on 24 | how to create custom CacheResources. 25 | 26 | > **Note** 27 | > 28 | > In Smarty2 this used to be a callback function called 29 | > `$cache_handler_func`. Smarty3 replaced this callback by the 30 | > `Smarty_CacheResource` module. 31 | 32 | 33 | registerCacheResource('mysql', new My_CacheResource_Mysql()); 35 | ?> 36 | 37 | 38 | 39 | See also [`unregisterCacheResource()`](#api.unregister.cacheresource) 40 | and the [Custom CacheResource Implementation](#caching.custom) section. 41 | -------------------------------------------------------------------------------- /src/Compile/Modifier/UnescapeModifierCompiler.php: -------------------------------------------------------------------------------- 1 | literal_compiler_param($params, 1, 'html'); 17 | 18 | if (!isset($params[ 2 ])) { 19 | $params[ 2 ] = '\'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\''; 20 | } 21 | 22 | switch ($esc_type) { 23 | case 'entity': 24 | case 'htmlall': 25 | return 'html_entity_decode(mb_convert_encoding(' . $params[ 0 ] . ', ' . $params[ 2 ] . ', \'UTF-8\'), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ' . $params[ 2 ] . ')'; 26 | case 'html': 27 | return 'htmlspecialchars_decode(' . $params[ 0 ] . ', ENT_QUOTES)'; 28 | case 'url': 29 | return 'rawurldecode(' . $params[ 0 ] . ')'; 30 | default: 31 | return $params[ 0 ]; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-debug-print-var.md: -------------------------------------------------------------------------------- 1 | # debug_print_var 2 | 3 | 4 | 5 | Returns the value of the given variable in a human-readable format in HTML. 6 | Used in the [debug console](../chapter-debugging-console.md), but you can also use it in your template 7 | while developing to see what is going on under the hood. 8 | 9 | > **Note** 10 | > 11 | > Use for debugging only! Since you may accidentally reveal sensitive information or introduce vulnerabilities such as XSS using this 12 | method never use it in production. 13 | 14 | ## Basic usage 15 | ```smarty 16 | {$myVar|debug_print_var} 17 | ``` 18 | 19 | 20 | ## Parameters 21 | 22 | | Parameter | Type | Required | Description | 23 | |-----------|------|----------|------------------------------------------------------------------------| 24 | | 1 | int | No | maximum recursion depth if $var is an array or object (defaults to 10) | 25 | | 2 | int | No | maximum string length if $var is a string (defaults to 40) | 26 | 27 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-count-paragraphs.md: -------------------------------------------------------------------------------- 1 | # count_paragraphs 2 | 3 | This is used to count the number of paragraphs in a variable. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {$myVar|count_paragraphs} 8 | ``` 9 | 10 | ## Examples 11 | 12 | ```php 13 | assign('articleTitle', 16 | "War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.\n\n 17 | Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation." 18 | ); 19 | 20 | ``` 21 | 22 | Where template is: 23 | 24 | ```smarty 25 | {$articleTitle} 26 | {$articleTitle|count_paragraphs} 27 | ``` 28 | 29 | 30 | Will output: 31 | 32 | ``` 33 | War Dims Hope for Peace. Child's Death Ruins Couple's Holiday. 34 | 35 | Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation. 36 | 2 37 | ``` 38 | 39 | See also [`count_characters`](language-modifier-count-characters.md), 40 | [`count_sentences`](language-modifier-count-sentences.md) and 41 | [`count_words`](language-modifier-count-words.md). 42 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-strip.md: -------------------------------------------------------------------------------- 1 | # strip 2 | 3 | This replaces all spaces, newlines and tabs with a single space, or with 4 | the supplied string. 5 | 6 | ## Basic usage 7 | ```smarty 8 | {$myVar|strip} 9 | ``` 10 | 11 | > **Note** 12 | > 13 | > If you want to strip blocks of template text, use the built-in 14 | > [`{strip}`](../language-builtin-functions/language-function-strip.md) function. 15 | 16 | ## Examples 17 | 18 | ```php 19 | assign('articleTitle', "Grandmother of\neight makes\t hole in one."); 21 | $smarty->display('index.tpl'); 22 | ``` 23 | 24 | Where template is: 25 | 26 | ```smarty 27 | {$articleTitle} 28 | {$articleTitle|strip} 29 | {$articleTitle|strip:' '} 30 | ``` 31 | 32 | Will output: 33 | 34 | ```html 35 | Grandmother of 36 | eight makes hole in one. 37 | Grandmother of eight makes hole in one. 38 | Grandmother of eight makes hole in one. 39 | ``` 40 | 41 | See also [`{strip}`](../language-builtin-functions/language-function-strip.md) and 42 | [`truncate`](language-modifier-truncate.md). 43 | -------------------------------------------------------------------------------- /src/Compile/Tag/Ldelim.php: -------------------------------------------------------------------------------- 1 | getAttributes($compiler, $args); 36 | if ($_attr['nocache'] === true) { 37 | $compiler->trigger_template_error('nocache option not allowed', null, true); 38 | } 39 | return $compiler->getTemplate()->getLeftDelimiter(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Compile/Tag/ForeachElse.php: -------------------------------------------------------------------------------- 1 | closeTag($compiler, ['foreach']); 27 | $this->openTag($compiler, 'foreachelse', ['foreachelse', $nocache_pushed, $localVariablePrefix, $item, false]); 28 | $output = "setVariable('{$item}', {$localVariablePrefix}Backup);\n"; 31 | } 32 | $output .= "}\nif ({$localVariablePrefix}DoElse) {\n?>"; 33 | return $output; 34 | } 35 | } -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-json-encode.md: -------------------------------------------------------------------------------- 1 | # json_encode 2 | 3 | Transforms a value into a valid JSON string. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {$user|json_encode} 8 | ``` 9 | Depending on the value of `$user` this would return a string in JSON-format, e.g. `{"username":"my_username","email":"my_username@smarty.net"}`. 10 | 11 | 12 | ## Parameters 13 | 14 | | Parameter | Type | Required | Description | 15 | |-----------|------|----------|-------------------------------------------------------------------------------------------| 16 | | 1 | int | No | bitmask of flags, directly passed to [PHP's json_encode](https://www.php.net/json_encode) | 17 | 18 | 19 | ## Examples 20 | 21 | By passing `16` as the second parameter, you can force json_encode to always format the JSON-string as an object. 22 | Without it, an array `$myArray = ["a","b"]` would be formatted as a javascript array: 23 | 24 | ```smarty 25 | {$myArray|json_encode} # renders: ["a","b"] 26 | {$myArray|json_encode:16} # renders: {"0":"a","1":"b"} 27 | ``` -------------------------------------------------------------------------------- /src/Compile/Tag/CaptureClose.php: -------------------------------------------------------------------------------- 1 | _cache['capture_stack'])) { 36 | // pop the virtual {nocache} tag from the stack. 37 | $compiler->closeTag('nocache'); 38 | $compiler->tag_nocache = true; 39 | } 40 | 41 | return "getSmarty()->getRuntime('Capture')->close(\$_smarty_tpl);?>"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-set-plugins-dir.md: -------------------------------------------------------------------------------- 1 | setPluginsDir() 2 | 3 | set the directories where plugins are stored 4 | 5 | Description 6 | =========== 7 | 8 | Smarty 9 | 10 | setPluginsDir 11 | 12 | string\|array 13 | 14 | plugins\_dir 15 | 16 | 17 | setPluginsDir('./plugins'); 21 | 22 | // view the plugins dir chain 23 | var_dump($smarty->getPluginsDir()); 24 | 25 | // set multiple directoríes where plugins are stored 26 | $smarty->setPluginsDir(array( 27 | './plugins', 28 | './plugins_2', 29 | )); 30 | 31 | // view the plugins dir chain 32 | var_dump($smarty->getPluginsDir()); 33 | 34 | // chaining of method calls 35 | $smarty->setTemplateDir('./templates') 36 | ->setPluginsDir('./plugins') 37 | ->setCompileDir('./templates_c') 38 | ->setCacheDir('./cache'); 39 | 40 | ?> 41 | 42 | 43 | 44 | See also [`getPluginsDir()`](#api.get.plugins.dir), 45 | [`addPluginsDir()`](#api.add.plugins.dir) and 46 | [`$plugins_dir`](#variable.plugins.dir). 47 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-compile-dir.md: -------------------------------------------------------------------------------- 1 | \$compile\_dir {#variable.compile.dir} 2 | ============== 3 | 4 | This is the name of the directory where compiled templates are located. 5 | By default this is `./templates_c`, meaning that Smarty will look for 6 | the `templates_c/` directory in the same directory as the executing php 7 | script. **This directory must be writeable by the web server**, [see 8 | install](#installing.smarty.basic) for more info. 9 | 10 | > **Note** 11 | > 12 | > This setting must be either a relative or absolute path. include\_path 13 | > is not used for writing files. 14 | 15 | > **Note** 16 | > 17 | > It is not recommended to put this directory under the web server 18 | > document root. 19 | 20 | > **Note** 21 | > 22 | > As of Smarty 3.1 the attribute \$compile\_dir is no longer accessible 23 | > directly. Use [`getCompileDir()`](#api.get.compile.dir) and 24 | > [`setCompileDir()`](#api.set.compile.dir) instead. 25 | 26 | See also [`getCompileDir()`](#api.get.compile.dir), 27 | [`setCompileDir()`](#api.set.compile.dir), 28 | [`$compile_id`](#variable.compile.id) and 29 | [`$use_sub_dirs`](#variable.use.sub.dirs). 30 | -------------------------------------------------------------------------------- /demo/index.php: -------------------------------------------------------------------------------- 1 | debugging = true; 11 | $smarty->caching = true; 12 | $smarty->cache_lifetime = 120; 13 | $smarty->assign("Name", "Fred Irving Johnathan Bradley Peppergill", true); 14 | $smarty->assign("FirstName", array("John", "Mary", "James", "Henry")); 15 | $smarty->assign("LastName", array("Doe", "Smith", "Johnson", "Case")); 16 | $smarty->assign( 17 | "Class", 18 | array( 19 | array("A", "B", "C", "D"), 20 | array("E", "F", "G", "H"), 21 | array("I", "J", "K", "L"), 22 | array("M", "N", "O", "P") 23 | ) 24 | ); 25 | $smarty->assign( 26 | "contacts", 27 | array( 28 | array("phone" => "1", "fax" => "2", "cell" => "3"), 29 | array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234") 30 | ) 31 | ); 32 | $smarty->assign("option_values", array("NY", "NE", "KS", "IA", "OK", "TX")); 33 | $smarty->assign("option_output", array("New York", "Nebraska", "Kansas", "Iowa", "Oklahoma", "Texas")); 34 | $smarty->assign("option_selected", "NE"); 35 | $smarty->display('index.tpl'); 36 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-template-dir.md: -------------------------------------------------------------------------------- 1 | \$template\_dir {#variable.template.dir} 2 | =============== 3 | 4 | This is the name of the default template directory. If you do not supply 5 | a resource type when including files, they will be found here. By 6 | default this is `./templates`, meaning that Smarty will look for the 7 | `templates/` directory in the same directory as the executing php 8 | script. \$template\_dir can also be an array of directory paths: Smarty 9 | will traverse the directories and stop on the first matching template 10 | found. 11 | 12 | > **Note** 13 | > 14 | > It is not recommended to put this directory under the web server 15 | > document root. 16 | 17 | > **Note** 18 | > As of Smarty 3.1 the attribute \$template\_dir is no longer accessible 19 | > directly. Use [`getTemplateDir()`](#api.get.template.dir), 20 | > [`setTemplateDir()`](#api.set.template.dir) and 21 | > [`addTemplateDir()`](#api.add.template.dir) instead. 22 | 23 | See also [`Template Resources`](#resources), 24 | [`getTemplateDir()`](#api.get.template.dir), 25 | [`setTemplateDir()`](#api.set.template.dir) and 26 | [`addTemplateDir()`](#api.add.template.dir). 27 | -------------------------------------------------------------------------------- /src/Compile/ObjectMethodBlockCompiler.php: -------------------------------------------------------------------------------- 1 | getSmarty()->registered_objects['{$tag}'][0]"; 26 | return "(isset({$callbackObject}) && is_callable(array({$callbackObject}, '{$function}')))"; 27 | } 28 | 29 | /** 30 | * @inheritDoc 31 | */ 32 | protected function getFullCallbackCode($tag, $function): string { 33 | $callbackObject = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]"; 34 | return "{$callbackObject}->{$function}"; 35 | } 36 | 37 | /** 38 | * @inheritDoc 39 | */ 40 | protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool { 41 | return true; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/Compile/Tag/SetfilterClose.php: -------------------------------------------------------------------------------- 1 | getAttributes($compiler, $args); 35 | 36 | // reset variable filter to previous state 37 | $compiler->getSmarty()->setDefaultModifiers( 38 | count($compiler->variable_filter_stack) ? array_pop($compiler->variable_filter_stack) : [] 39 | ); 40 | 41 | return ''; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Compile/Tag/IfClose.php: -------------------------------------------------------------------------------- 1 | closeTag($compiler, ['if', 'else', 'elseif']); 35 | 36 | if ($nocache_pushed) { 37 | // pop the pushed virtual nocache tag 38 | $this->closeTag($compiler, 'nocache'); 39 | $compiler->tag_nocache = true; 40 | } 41 | 42 | $tmp = ''; 43 | for ($i = 0; $i < $nesting; $i++) { 44 | $tmp .= '}'; 45 | } 46 | return ""; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-clear-compiled-tpl.md: -------------------------------------------------------------------------------- 1 | clearCompiledTemplate() 2 | 3 | clears the compiled version of the specified template resource 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | clearCompiledTemplate 11 | 12 | string 13 | 14 | tpl\_file 15 | 16 | string 17 | 18 | compile\_id 19 | 20 | int 21 | 22 | exp\_time 23 | 24 | This clears the compiled version of the specified template resource, or 25 | all compiled template files if one is not specified. If you pass a 26 | [`$compile_id`](#variable.compile.id) only the compiled template for 27 | this specific [`$compile_id`](#variable.compile.id) is cleared. If you 28 | pass an exp\_time, then only compiled templates older than `exp_time` 29 | seconds are cleared, by default all compiled templates are cleared 30 | regardless of their age. This function is for advanced use only, not 31 | normally needed. 32 | 33 | 34 | clearCompiledTemplate('index.tpl'); 37 | 38 | // clear entire compile directory 39 | $smarty->clearCompiledTemplate(); 40 | ?> 41 | 42 | 43 | 44 | See also [`clearCache()`](#api.clear.cache). 45 | -------------------------------------------------------------------------------- /docs/api/filters/output-filters.md: -------------------------------------------------------------------------------- 1 | # Output filters 2 | 3 | When a template is rendered, its output can be sent through one or more 4 | output filters. 5 | 6 | > **Note** 7 | > This differs from [`prefilters`](prefilters.md) and 8 | > [`postfilters`](postfilters.md) because, pre- and postfilters 9 | > operate on compiled templates before they are saved to the disk, whereas 10 | > output filters operate on the template output when it is executed. 11 | 12 | Smarty will pass the template output as the first argument, and expect the function 13 | to return the result of the processing. 14 | 15 | Output filters can be either added as part of an [Extension](../extending/extensions.md) or 16 | registered as shown below. 17 | 18 | This will provide a rudimentary protection against spambots: 19 | ```php 20 | registerFilter("output", "protect_email"); 33 | $smarty->display("index.tpl'); 34 | 35 | ``` 36 | -------------------------------------------------------------------------------- /src/Compile/Tag/Debug.php: -------------------------------------------------------------------------------- 1 | getAttributes($compiler, $args); 36 | 37 | // compile always as nocache 38 | $compiler->tag_nocache = true; 39 | // display debug template 40 | $_output = 41 | "display_debug(\$_smarty_tpl);\n"; 42 | $_output .= "unset(\$_smarty_debug);\n?>"; 43 | return $_output; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-split.md: -------------------------------------------------------------------------------- 1 | # split 2 | 3 | Splits a string into an array, using the optional second parameter as the separator. 4 | 5 | ## Basic usage 6 | 7 | For `$chars` populated with `'abc'`, the following will produce a html list with 3 elements (a, b and c). 8 | ```smarty 9 |
    10 | {foreach $chars|split as $char} 11 |
  1. {$char|escape}
  2. 12 | {/foreach} 13 |
14 | ``` 15 | 16 | ## Parameters 17 | 18 | | Parameter | Type | Required | Description | 19 | |-----------|--------|----------|------------------------------------------------------------------------------------------------------------------------------| 20 | | 1 | string | No | separator used to split the string on. Defaults to empty string, causing each character in the source string to be separate. | 21 | 22 | ## Examples 23 | 24 | 25 | For `$ids` populated with `'1,2,3'`, the following will produce a html list with 3 elements (1, 2 and 3). 26 | ```smarty 27 |
    28 | {foreach $ids|split:',' as $id} 29 |
  1. {$id|escape}
  2. 30 | {/foreach} 31 |
32 | ``` -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-count-characters.md: -------------------------------------------------------------------------------- 1 | # count_characters 2 | 3 | This is used to count the number of characters in a variable. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {$myVar|count_characters} 8 | ``` 9 | 10 | ## Parameters 11 | 12 | | Parameter | Type | Required | Description | 13 | |-----------|---------|----------|------------------------------------------------------------------------| 14 | | 1 | boolean | No | This determines whether to include whitespace characters in the count. | 15 | 16 | ## Examples 17 | 18 | ```php 19 | assign('articleTitle', 'Cold Wave Linked to Temperatures.'); 22 | 23 | ``` 24 | 25 | Where template is: 26 | 27 | ```smarty 28 | {$articleTitle} 29 | {$articleTitle|count_characters} 30 | {$articleTitle|count_characters:true} 31 | ``` 32 | 33 | Will output: 34 | 35 | ``` 36 | Cold Wave Linked to Temperatures. 37 | 29 38 | 33 39 | ``` 40 | 41 | See also [`count_words`](language-modifier-count-words.md), 42 | [`count_sentences`](language-modifier-count-sentences.md) and 43 | [`count_paragraphs`](language-modifier-count-paragraphs.md). 44 | -------------------------------------------------------------------------------- /src/FunctionHandler/Count.php: -------------------------------------------------------------------------------- 1 | 2) { 23 | throw new Exception("Invalid number of arguments for count. count expects 1 or 2 parameters."); 24 | } 25 | 26 | $value = $params[0]; 27 | 28 | if ($value instanceof \Countable) { 29 | return $value->count(); 30 | } 31 | 32 | $mode = count($params) == 2 ? (int) $params[1] : COUNT_NORMAL; 33 | return count((array) $value, $mode); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /docs/designers/language-basic-syntax/index.md: -------------------------------------------------------------------------------- 1 | # Basic Syntax 2 | 3 | A simple Smarty template could look like this: 4 | ```smarty 5 |

{$title|escape}

6 | 13 | ``` 14 | 15 | All Smarty template tags are enclosed within delimiters. By default 16 | these are `{` and `}`, but they can be 17 | [changed](../../designers/language-basic-syntax/language-escaping.md). 18 | 19 | For the examples in this manual, we will assume that you are using the 20 | default delimiters. In Smarty, all content outside of delimiters is 21 | displayed as static content, or unchanged. When Smarty encounters 22 | template tags, it attempts to interpret them, and displays the 23 | appropriate output in their place. 24 | 25 | The basic components of the Smarty syntax are: 26 | 27 | - [Comments](language-syntax-comments.md) 28 | - [Variables](language-syntax-variables.md) 29 | - [Operators](language-syntax-operators.md) 30 | - [Tags](language-syntax-tags.md) 31 | - [Attributes](language-syntax-attributes.md) 32 | - [Quotes](language-syntax-quotes.md) 33 | - [Escaping](language-escaping.md) 34 | -------------------------------------------------------------------------------- /src/Compile/Tag/SectionClose.php: -------------------------------------------------------------------------------- 1 | loopNesting--; 31 | 32 | [$openTag, $nocache_pushed] = $this->closeTag($compiler, ['section', 'sectionelse']); 33 | 34 | if ($nocache_pushed) { 35 | // pop the pushed virtual nocache tag 36 | $this->closeTag($compiler, 'nocache'); 37 | } 38 | 39 | $output = "'; 46 | return $output; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Compile/Tag/ForClose.php: -------------------------------------------------------------------------------- 1 | loopNesting--; 35 | 36 | [$openTag, $nocache_pushed] = $this->closeTag($compiler, ['for', 'forelse']); 37 | $output = ""; 42 | 43 | if ($nocache_pushed) { 44 | // pop the pushed virtual nocache tag 45 | $this->closeTag($compiler, 'nocache'); 46 | $compiler->tag_nocache = true; 47 | } 48 | 49 | return $output; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-round.md: -------------------------------------------------------------------------------- 1 | # round 2 | 3 | Rounds a number to the specified precision. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {3.14|round} # renders: 3 8 | ``` 9 | 10 | ```smarty 11 | {3.141592|round:2} # renders: 3.14 12 | ``` 13 | 14 | ## Parameters 15 | 16 | | Parameter | Type | Required | Description | 17 | |-----------|------|----------|---------------------------| 18 | | 1 | int | No | precision (defaults to 0) | 19 | | 2 | int | No | mode (defaults to 1) | 20 | 21 | If 'precision' is negative, the number is rounded to the nearest power of 10. See examples below. 22 | 23 | The parameter 'mode' defines how the rounding is done. By default, 2.5 is rounded to 3, whereas 2.45 is rounded to 2. 24 | You usually don't need to change this. For more details on rounding modes, 25 | see [PHP's documentation on round](https://www.php.net/manual/en/function.round). 26 | 27 | ## Examples 28 | 29 | By passing `16` as the second parameter, you can force json_encode to always format the JSON-string as an object. 30 | Without it, an array `$myArray = ["a","b"]` would be formatted as a javascript array: 31 | 32 | ```smarty 33 | {$myArray|json_encode} # renders: ["a","b"] 34 | {$myArray|json_encode:16} # renders: {"0":"a","1":"b"} 35 | ``` -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-config-overwrite.md: -------------------------------------------------------------------------------- 1 | \$config\_overwrite {#variable.config.overwrite} 2 | =================== 3 | 4 | If set to TRUE, the default then variables read in from [config 5 | files](#config.files) will overwrite each other. Otherwise, the 6 | variables will be pushed onto an array. This is helpful if you want to 7 | store arrays of data in config files, just list each element multiple 8 | times. 9 | 10 | This examples uses [`{cycle}`](#language.function.cycle) to output a 11 | table with alternating red/green/blue row colors with 12 | `$config_overwrite` = FALSE. 13 | 14 | The config file. 15 | 16 | 17 | # row colors 18 | rowColors = #FF0000 19 | rowColors = #00FF00 20 | rowColors = #0000FF 21 | 22 | 23 | 24 | The template with a [`{section}`](#language.function.section) loop. 25 | 26 | 27 | 28 | {section name=r loop=$rows} 29 | 30 | 31 | 32 | {/section} 33 |
....etc....
34 | 35 | 36 | 37 | See also [`{config_load}`](#language.function.config.load), 38 | [`getConfigVars()`](#api.get.config.vars), 39 | [`clearConfig()`](#api.clear.config), [`configLoad()`](#api.config.load) 40 | and the [config files section](#config.files). 41 | -------------------------------------------------------------------------------- /src/Compile/Tag/Setfilter.php: -------------------------------------------------------------------------------- 1 | variable_filter_stack[] = $compiler->getSmarty()->getDefaultModifiers(); 27 | 28 | // The modifier_list is passed as an array of array's. The inner arrays have the modifier at index 0, 29 | // and, possibly, parameters at subsequent indexes, e.g. [ ['escape','"mail"'] ] 30 | // We will collapse them so the syntax is OK for ::setDefaultModifiers() as follows: [ 'escape:"mail"' ] 31 | $newList = []; 32 | foreach($parameter['modifier_list'] as $modifier) { 33 | $newList[] = implode(':', $modifier); 34 | } 35 | 36 | $compiler->getSmarty()->setDefaultModifiers($newList); 37 | 38 | return ''; 39 | } 40 | } -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-replace.md: -------------------------------------------------------------------------------- 1 | # replace 2 | 3 | A simple search and replace on a variable. This is equivalent to the 4 | PHP's [`str_replace()`](https://www.php.net/str_replace) function. 5 | 6 | ## Basic usage 7 | ```smarty 8 | {$myVar|replace:"foo":"bar"} 9 | ``` 10 | 11 | ## Parameters 12 | 13 | | Parameter Position | Type | Required | Description | 14 | |--------------------|--------|----------|---------------------------------------------| 15 | | 1 | string | Yes | This is the string of text to be replaced. | 16 | | 2 | string | Yes | This is the string of text to replace with. | 17 | 18 | 19 | ## Examples 20 | 21 | ```php 22 | assign('articleTitle', "Child's Stool Great for Use in Garden."); 25 | 26 | ``` 27 | 28 | Where template is: 29 | 30 | ```smarty 31 | {$articleTitle} 32 | {$articleTitle|replace:'Garden':'Vineyard'} 33 | {$articleTitle|replace:' ':' '} 34 | ``` 35 | 36 | Will output: 37 | 38 | ``` 39 | Child's Stool Great for Use in Garden. 40 | Child's Stool Great for Use in Vineyard. 41 | Child's Stool Great for Use in Garden. 42 | ``` 43 | 44 | See also [`regex_replace`](language-modifier-regex-replace.md) and 45 | [`escape`](language-modifier-escape.md). 46 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-config-load.md: -------------------------------------------------------------------------------- 1 | configLoad() 2 | 3 | loads config file data and assigns it to the template 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | configLoad 11 | 12 | string 13 | 14 | file 15 | 16 | string 17 | 18 | section 19 | 20 | This loads [config file](#config.files) data and assigns it to the 21 | template. This works identically to the template 22 | [`{config_load}`](#language.function.config.load) function. 23 | 24 | > **Note** 25 | > 26 | > As of Smarty 2.4.0, assigned template variables are kept across 27 | > invocations of [`fetch()`](#api.fetch) and 28 | > [`display()`](#api.display). Config vars loaded from `configLoad()` 29 | > are always global in scope. Config files are also compiled for faster 30 | > execution, and respect the [`$force_compile`](#variable.force.compile) 31 | > and [`$compile_check`](#variable.compile.check) settings. 32 | 33 | 34 | configLoad('my.conf'); 37 | 38 | // load a section 39 | $smarty->configLoad('my.conf', 'foobar'); 40 | ?> 41 | 42 | 43 | 44 | See also [`{config_load}`](#language.function.config.load), 45 | [`getConfigVars()`](#api.get.config.vars), 46 | [`clearConfig()`](#api.clear.config), and 47 | [`config variables`](#language.config.variables) 48 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-register-resource.md: -------------------------------------------------------------------------------- 1 | registerResource() 2 | 3 | dynamically register resources 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | registerResource 11 | 12 | string 13 | 14 | name 15 | 16 | Smarty\_resource 17 | 18 | resource\_handler 19 | 20 | Use this to dynamically register a [Resource plugin](#resources) with 21 | Smarty. Pass in the `name` of the Resource and the object extending 22 | Smarty\_Resource. See [template resources](#resources) for more 23 | information on how to setup a function for fetching templates. 24 | 25 | > **Note** 26 | > 27 | > A resource name must be at least two characters in length. One 28 | > character resource names will be ignored and used as part of the file 29 | > path, such as `$smarty->display('c:/path/to/index.tpl');` 30 | 31 | > **Note** 32 | > 33 | > Prior to Smarty 3.1 `registerResource()` accepted an array of callback 34 | > functions. While this is still possible for backward compatibility 35 | > reasons, it is strongly discouraged as callback functions have been 36 | > deprecated as of Smarty 3.1. 37 | 38 | 39 | registerResource('mysql', new My_Resource_Mysql()); 41 | ?> 42 | 43 | 44 | 45 | See also [`unregisterResource()`](#api.unregister.resource) and the 46 | [template resources](#resources) section. 47 | -------------------------------------------------------------------------------- /docs/designers/language-variables/index.md: -------------------------------------------------------------------------------- 1 | # Variables 2 | 3 | Smarty has several types of variables. The type of the 4 | variable depends on what symbol it is prefixed or enclosed within. 5 | 6 | - [Variables assigned from PHP](language-assigned-variables.md) 7 | - [Variables loaded from config files](language-config-variables.md) 8 | - [{$smarty} reserved variable](language-variables-smarty.md) 9 | 10 | Variables in Smarty can be either displayed directly or used as 11 | arguments for [tags](../language-basic-syntax/language-syntax-tags.md), 12 | [attributes](../language-basic-syntax/language-syntax-attributes.md) and 13 | [modifiers](../language-modifiers/index.md), inside conditional expressions, etc. 14 | To print a variable, simply enclose it in the 15 | [delimiters](../../designers/language-basic-syntax/language-escaping.md) so that it is the only thing 16 | contained between them. 17 | 18 | ```smarty 19 | {$Name} 20 | 21 | {$product.part_no} {$product.description} 22 | 23 | {$Contacts[row].Phone} 24 | 25 | 26 | ``` 27 | 28 | ## Scopes 29 | You can assign variables to specific [variable scopes](language-variable-scopes.md). 30 | 31 | 32 | > **Note** 33 | > 34 | > An easy way to examine assigned Smarty variables is with the 35 | > [debugging console](../chapter-debugging-console.md). 36 | 37 | -------------------------------------------------------------------------------- /TODO.txt: -------------------------------------------------------------------------------- 1 | # @TODO 2 | 3 | ## CI-building optimization 4 | - compiled & cached templates should not contain references to local filesystem paths. Add an optional rootpath param 5 | to `(add|set)TemplateDir` or as a separate method. Make it default to `getcwd()`. If a relative path is passed to 6 | `(add|set)TemplateDir`, prefix it with the rootpath at runtime, but do not store the path. 7 | 8 | ## Review direct variable property access 9 | - review ->value{$index} in ForTag 10 | 11 | ## include inline 12 | - Re-introduce merge_compiled_includes and the {include inline} attribute? 13 | 14 | ## Output buffering 15 | - Fix ob_ output buffering commands being scattered around the codebase 16 | 17 | ## Review public static vars 18 | - such as _CHARSET and _IS_WINDOWS 19 | 20 | ## Block / inheritance 21 | - Consider phasing out $smarty.block.child as this reverses the inheritance hierarchy and might cause infinite loops 22 | when combined with $smarty.block.parent 23 | 24 | ## Plugin system 25 | - fix template security checks in one place in compiler 26 | 27 | ## Beatify output 28 | - compiled templates could be proper classes, possibly using [nette/php-generator](https://packagist.org/packages/nette/php-generator) 29 | 30 | ## Unrelated / other 31 | - review (and avoid) use of 'clone' keyword 32 | - what is 'user literal support', why are unit tests skipped? 33 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-spacify.md: -------------------------------------------------------------------------------- 1 | # spacify 2 | 3 | `spacify` is a way to insert a space between every character of a 4 | variable. You can optionally pass a different character or string to 5 | insert. 6 | 7 | ## Basic usage 8 | ```smarty 9 | {$myVar|spacify} 10 | ``` 11 | 12 | ## Parameters 13 | 14 | | Parameter Position | Type | Required | Default | Description | 15 | |--------------------|--------|----------|-------------|-----------------------------------------------------------------| 16 | | 1 | string | No | *one space* | This what gets inserted between each character of the variable. | 17 | 18 | ## Examples 19 | 20 | ```php 21 | assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.'); 24 | 25 | ``` 26 | 27 | Where template is: 28 | 29 | ```smarty 30 | {$articleTitle} 31 | {$articleTitle|spacify} 32 | {$articleTitle|spacify:"^^"} 33 | ``` 34 | 35 | Will output: 36 | 37 | ``` 38 | Something Went Wrong in Jet Crash, Experts Say. 39 | S o m e t h i n g W .... snip .... s h , E x p e r t s S a y . 40 | S^^o^^m^^e^^t^^h^^i^^n^^g^^ .... snip .... ^^e^^r^^t^^s^^ ^^S^^a^^y^^. 41 | ``` 42 | 43 | See also [`wordwrap`](language-modifier-wordwrap.md) and 44 | [`nl2br`](language-modifier-nl2br.md). 45 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-default.md: -------------------------------------------------------------------------------- 1 | # default 2 | 3 | This is used to set a default value for a variable. If the variable is 4 | unset or an empty string, the given default value is printed instead. 5 | Default takes the one argument. 6 | 7 | ## Basic usage 8 | ```smarty 9 | {$myVar|default:"(none)"} 10 | ``` 11 | 12 | ## Parameters 13 | 14 | | Parameter | Type | Required | Default | Description | 15 | |-----------|--------|----------|---------|---------------------------------------------------------------| 16 | | 1 | string | No | *empty* | This is the default value to output if the variable is empty. | 17 | 18 | ## Examples 19 | 20 | ```php 21 | assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.'); 24 | $smarty->assign('email', ''); 25 | 26 | ``` 27 | 28 | Where template is: 29 | 30 | ```smarty 31 | {$articleTitle|default:'no title'} 32 | {$myTitle|default:'no title'} 33 | {$email|default:'No email address available'} 34 | ``` 35 | 36 | Will output: 37 | 38 | ``` 39 | Dealers Will Hear Car Talk at Noon. 40 | no title 41 | No email address available 42 | ``` 43 | 44 | See also the [default variable handling](../../appendixes/tips.md#default-variable-handling) and 45 | the [blank variable handling](../../appendixes/tips.md#blank-variable-handling) pages. 46 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-literal.md: -------------------------------------------------------------------------------- 1 | # {literal} 2 | 3 | `{literal}` tags allow a block of data to be taken literally. This is 4 | typically used around Javascript or stylesheet blocks where {curly 5 | braces} would interfere with the template 6 | [delimiter](../../designers/language-basic-syntax/language-escaping.md) syntax. Anything within 7 | `{literal}{/literal}` tags is not interpreted, but displayed as-is. If 8 | you need template tags embedded in a `{literal}` block, consider using 9 | [`{ldelim}{rdelim}`](./language-function-ldelim.md) to escape the individual 10 | delimiters instead. 11 | 12 | > **Note** 13 | > 14 | > `{literal}{/literal}` tags are normally not necessary, as Smarty 15 | > ignores delimiters that are surrounded by whitespace. Be sure your 16 | > javascript and CSS curly braces are surrounded by whitespace. This is 17 | > new behavior to Smarty 3. 18 | 19 | ```smarty 20 | 31 | ``` 32 | 33 | See also [`{ldelim} {rdelim}`](./language-function-ldelim.md) and the 34 | [escaping Smarty parsing](../language-basic-syntax/language-escaping.md) page. 35 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-create-data.md: -------------------------------------------------------------------------------- 1 | createData() 2 | 3 | creates a data object 4 | 5 | Description 6 | =========== 7 | 8 | string 9 | 10 | createData 11 | 12 | object 13 | 14 | parent 15 | 16 | string 17 | 18 | createData 19 | 20 | This creates a data object which will hold assigned variables. It uses 21 | the following parameters: 22 | 23 | - `parent` is an optional parameter. It is an uplink to the main 24 | Smarty object, a another user-created data object or to user-created 25 | template object. These objects can be chained. Templates can access 26 | variables assigned to any of the objects in it\'s parent chain. 27 | 28 | Data objects are used to create scopes for assigned variables. They can 29 | be used to control which variables are seen by which templates. 30 | 31 | 32 | createData(); 38 | 39 | // assign variable to data scope 40 | $data->assign('foo','bar'); 41 | 42 | // create template object which will use variables from data object 43 | $tpl = $smarty->createTemplate('index.tpl',$data); 44 | 45 | // display the template 46 | $tpl->display(); 47 | ?> 48 | 49 | 50 | 51 | See also [`display()`](#api.display), and 52 | [`createTemplate()`](#api.create.template), 53 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-to-charset.md: -------------------------------------------------------------------------------- 1 | # to_charset 2 | 3 | `to_charset` is used to transcode a string from the internal charset to 4 | a given charset. This is the exact opposite of the [from_charset 5 | modifier](#language.modifier.from_charset). 6 | 7 | ## Parameters 8 | 9 | | Parameter Position | Type | Required | Possible Values | Default | Description | 10 | |--------------------|--------|----------|------------------------------------------------------------------------------------------------------------------------------|--------------|-------------------------------------------------------------| 11 | | 1 | string | No | `ISO-8859-1`, `UTF-8`, and any character set supported by [`mb_convert_encoding()`](https://www.php.net/mb_convert_encoding) | `ISO-8859-1` | The charset encoding the value is supposed to be encoded to | 12 | 13 | > **Note** 14 | > 15 | > Charset encoding should be handled by the application itself. This 16 | > modifier should only be used in cases where the application cannot 17 | > anticipate that a certain string is required in another encoding. 18 | 19 | See also [Configuring Smarty](../../api/configuring.md), [from_charset 20 | modifier](language-modifier-from-charset.md). 21 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-from-charset.md: -------------------------------------------------------------------------------- 1 | # from_charset 2 | 3 | `from_charset` is used to transcode a string from a given charset to the 4 | internal charset. This is the exact opposite of the [to_charset 5 | modifier](language-modifier-to-charset.md). 6 | 7 | ## Parameters 8 | 9 | | Parameter Position | Type | Required | Possible Values | Default | Description | 10 | |--------------------|--------|----------|------------------------------------------------------------------------------------------------------------------------------|--------------|---------------------------------------------------------------| 11 | | 1 | string | No | `ISO-8859-1`, `UTF-8`, and any character set supported by [`mb_convert_encoding()`](https://www.php.net/mb_convert_encoding) | `ISO-8859-1` | The charset encoding the value is supposed to be decoded from | 12 | 13 | > **Note** 14 | > 15 | > Charset encoding should be handled by the application itself. This 16 | > modifier should only be used in cases where the application cannot 17 | > anticipate that a certain string is required in another encoding. 18 | 19 | See also [Configuring Smarty](../../api/configuring.md), [to_charset 20 | modifier](language-modifier-to-charset.md). 21 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-append.md: -------------------------------------------------------------------------------- 1 | append() 2 | 3 | append an element to an assigned array 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | append 11 | 12 | mixed 13 | 14 | var 15 | 16 | void 17 | 18 | append 19 | 20 | string 21 | 22 | varname 23 | 24 | mixed 25 | 26 | var 27 | 28 | bool 29 | 30 | merge 31 | 32 | If you append to a string value, it is converted to an array value and 33 | then appended to. You can explicitly pass name/value pairs, or 34 | associative arrays containing the name/value pairs. If you pass the 35 | optional third parameter of TRUE, the value will be merged with the 36 | current array instead of appended. 37 | 38 | NOTE.PARAMETER.MERGE 39 | 40 | 41 | append('foo', 'Fred'); 44 | // After this line, foo will now be seen as an array in the template 45 | $smarty->append('foo', 'Albert'); 46 | 47 | $array = array(1 => 'one', 2 => 'two'); 48 | $smarty->append('X', $array); 49 | $array2 = array(3 => 'three', 4 => 'four'); 50 | // The following line will add a second element to the X array 51 | $smarty->append('X', $array2); 52 | 53 | // passing an associative array 54 | $smarty->append(array('city' => 'Lincoln', 'state' => 'Nebraska')); 55 | ?> 56 | 57 | 58 | 59 | See also [`assign()`](#api.assign) and 60 | [`getTemplateVars()`](#api.get.template.vars) 61 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-cache-dir.md: -------------------------------------------------------------------------------- 1 | \$cache\_dir {#variable.cache.dir} 2 | ============ 3 | 4 | This is the name of the directory where template caches are stored. By 5 | default this is `./cache`, meaning that Smarty will look for the 6 | `cache/` directory in the same directory as the executing php script. 7 | **This directory must be writeable by the web server**, [see 8 | install](#installing.smarty.basic) for more info. 9 | 10 | You can also use your own [custom cache implementation](#caching.custom) 11 | to control cache files, which will ignore this setting. See also 12 | [`$use_sub_dirs`](#variable.use.sub.dirs). 13 | 14 | > **Note** 15 | > 16 | > This setting must be either a relative or absolute path. include\_path 17 | > is not used for writing files. 18 | 19 | > **Note** 20 | > 21 | > It is not recommended to put this directory under the web server 22 | > document root. 23 | 24 | > **Note** 25 | > 26 | > As of Smarty 3.1 the attribute \$cache\_dir is no longer accessible 27 | > directly. Use [`getCacheDir()`](#api.get.cache.dir) and 28 | > [`setCacheDir()`](#api.set.cache.dir) instead. 29 | 30 | See also [`getCacheDir()`](#api.get.cache.dir), 31 | [`setCacheDir()`](#api.set.cache.dir), [`$caching`](#variable.caching), 32 | [`$use_sub_dirs`](#variable.use.sub.dirs), 33 | [`$cache_lifetime`](#variable.cache.lifetime), 34 | [`$cache_modified_check`](#variable.cache.modified.check) and the 35 | [caching section](#caching). 36 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-use-sub-dirs.md: -------------------------------------------------------------------------------- 1 | \$use\_sub\_dirs {#variable.use.sub.dirs} 2 | ================ 3 | 4 | Smarty will create subdirectories under the [compiled 5 | templates](#variable.compile.dir) and [cache](#variable.cache.dir) 6 | directories if `$use_sub_dirs` is set to TRUE, default is FALSE. In an 7 | environment where there are potentially tens of thousands of files 8 | created, this may help the filesystem speed. On the other hand, some 9 | environments do not allow PHP processes to create directories, so this 10 | must be disabled which is the default. 11 | 12 | Sub directories are more efficient, so use them if you can. 13 | Theoretically you get much better performance on a filesystem with 10 14 | directories each having 100 files, than with 1 directory having 1000 15 | files. This was certainly the case with Solaris 7 (UFS)\... with newer 16 | filesystems such as ext3 and especially reiserfs, the difference is 17 | almost nothing. 18 | 19 | > **Note** 20 | > 21 | > - `$use_sub_dirs=true` doesn\'t work with 22 | > [safe\_mode=On](https://www.php.net/features.safe-mode), that\'s why 23 | > it\'s switchable and why it\'s off by default. 24 | > 25 | > - `$use_sub_dirs=true` on Windows can cause problems. 26 | > 27 | > - Safe\_mode is being deprecated in PHP6. 28 | > 29 | See also [`$compile_id`](#variable.compile.id), 30 | [`$cache_dir`](#variable.cache.dir), and 31 | [`$compile_dir`](#variable.compile.dir). 32 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-register-filter.md: -------------------------------------------------------------------------------- 1 | registerFilter() 2 | 3 | dynamically register filters 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | registerFilter 11 | 12 | string 13 | 14 | type 15 | 16 | mixed 17 | 18 | callback 19 | 20 | Use this to dynamically register filters to operate on a templates. It 21 | uses the following parameters: 22 | 23 | NOTE.PARAMETER.FUNCTION 24 | 25 | A [prefilter](#plugins.prefilters.postfilters) runs through the template 26 | source before it gets compiled. See [template 27 | prefilters](#advanced.features.prefilters) for more information on how 28 | to setup a prefiltering function. 29 | 30 | A [postfilter](#plugins.prefilters.postfilters) runs through the 31 | template code after it was compiled to PHP. See [template 32 | postfilters](#advanced.features.postfilters) for more information on how 33 | to setup a postfiltering function. 34 | 35 | A [outputfilter](#plugins.outputfilters) operates on a template\'s 36 | output before it is [displayed](#api.display). See [template output 37 | filters](#advanced.features.outputfilters) for more information on how 38 | to set up an output filter function. 39 | 40 | See also [`unregisterFilter()`](#api.unregister.filter), 41 | [`loadFilter()`](#api.load.filter), [template pre 42 | filters](#advanced.features.prefilters) [template post 43 | filters](#advanced.features.postfilters) [template output 44 | filters](#advanced.features.outputfilters) section. 45 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-cache-lifetime.md: -------------------------------------------------------------------------------- 1 | \$cache\_lifetime {#variable.cache.lifetime} 2 | ================= 3 | 4 | This is the length of time in seconds that a template cache is valid. 5 | Once this time has expired, the cache will be regenerated. 6 | 7 | - `$caching` must be turned on (either 8 | \Smarty\Smarty::CACHING\_LIFETIME\_CURRENT or 9 | \Smarty\Smarty::CACHING\_LIFETIME\_SAVED) for `$cache_lifetime` to have any 10 | purpose. 11 | 12 | - A `$cache_lifetime` value of -1 will force the cache to never 13 | expire. 14 | 15 | - A value of 0 will cause the cache to always regenerate (good for 16 | testing only, to disable caching a more efficient method is to set 17 | [`$caching`](#variable.caching) = \Smarty\Smarty::CACHING\_OFF). 18 | 19 | - If you want to give certain templates their own cache lifetime, you 20 | could do this by setting [`$caching`](#variable.caching) = 21 | \Smarty\Smarty::CACHING\_LIFETIME\_SAVED, then set `$cache_lifetime` to a 22 | unique value just before calling [`display()`](#api.display) or 23 | [`fetch()`](#api.fetch). 24 | 25 | If [`$force_compile`](#variable.force.compile) is enabled, the cache 26 | files will be regenerated every time, effectively disabling caching. You 27 | can clear all the cache files with the 28 | [`clear_all_cache()`](#api.clear.all.cache) function, or individual 29 | cache files (or groups) with the [`clear_cache()`](#api.clear.cache) 30 | function. 31 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-strip-tags.md: -------------------------------------------------------------------------------- 1 | # strip_tags 2 | 3 | This strips out HTML markup tags, basically anything between `<` and `>`. 4 | 5 | ## Basic usage 6 | ```smarty 7 | {$myVar|strip_tags} 8 | ``` 9 | 10 | ## Parameters 11 | 12 | | Parameter Position | Type | Required | Default | Description | 13 | |--------------------|------|----------|---------|------------------------------------------------------------| 14 | | 1 | bool | No | TRUE | This determines whether the tags are replaced by ' ' or '' | 15 | 16 | ## Examples 17 | 18 | ```php 19 | assign('articleTitle', 22 | "Blind Woman Gets New 23 | Kidney from Dad she Hasn't Seen in years." 24 | ); 25 | 26 | ``` 27 | 28 | 29 | Where template is: 30 | 31 | ```smarty 32 | {$articleTitle} 33 | {$articleTitle|strip_tags} {* same as {$articleTitle|strip_tags:true} *} 34 | {$articleTitle|strip_tags:false} 35 | ``` 36 | 37 | Will output: 38 | 39 | ```html 40 | Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. 41 | Blind Woman Gets New Kidney from Dad she Hasn't Seen in years . 42 | Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. 43 | ``` 44 | 45 | See also [`replace`](language-modifier-replace.md) and 46 | [`regex_replace`](language-modifier-regex-replace.md). 47 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-compile-all-config.md: -------------------------------------------------------------------------------- 1 | compileAllConfig() 2 | 3 | compiles all known config files 4 | 5 | Description 6 | =========== 7 | 8 | string 9 | 10 | compileAllConfig 11 | 12 | string 13 | 14 | extension 15 | 16 | boolean 17 | 18 | force 19 | 20 | integer 21 | 22 | timelimit 23 | 24 | integer 25 | 26 | maxerror 27 | 28 | This function compiles config files found in the 29 | [`$config_dir`](#variable.config.dir) folder. It uses the following 30 | parameters: 31 | 32 | - `extension` is an optional string which defines the file extension 33 | for the config files. The default is \".conf\". 34 | 35 | - `force` is an optional boolean which controls if only modified 36 | (false) or all (true) config files shall be compiled. The default is 37 | \"false\". 38 | 39 | - `timelimit` is an optional integer to set a runtime limit in seconds 40 | for the compilation process. The default is no limit. 41 | 42 | - `maxerror` is an optional integer to set an error limit. If more 43 | config files failed to compile the function will be aborted. The 44 | default is no limit. 45 | 46 | > **Note** 47 | > 48 | > This function may not create desired results in all configurations. 49 | > Use is on own risk. 50 | 51 | 52 | compileAllConfig('.config',true); 58 | 59 | ?> 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/ParseTree/Text.php: -------------------------------------------------------------------------------- 1 | data = $data; 37 | $this->toBeStripped = $toBeStripped; 38 | } 39 | 40 | /** 41 | * Wether this section should be stripped on output to smarty php 42 | * @return bool 43 | */ 44 | public function isToBeStripped() { 45 | return $this->toBeStripped; 46 | } 47 | 48 | /** 49 | * Return buffer content 50 | * 51 | * @param \Smarty\Parser\TemplateParser $parser 52 | * 53 | * @return string text 54 | */ 55 | public function to_smarty_php(\Smarty\Parser\TemplateParser $parser) 56 | { 57 | return $this->data; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-ldelim.md: -------------------------------------------------------------------------------- 1 | # {ldelim}, {rdelim} 2 | 3 | `{ldelim}` and `{rdelim}` are used for [escaping](../language-basic-syntax/language-escaping.md) 4 | template delimiters, by default **{** and **}**. You can also use 5 | [`{literal}{/literal}`](./language-function-literal.md) to escape blocks of 6 | text eg Javascript or CSS. See also the complementary 7 | [`{$smarty.ldelim}`](../../designers/language-basic-syntax/language-escaping.md). 8 | 9 | ```smarty 10 | {* this will print literal delimiters out of the template *} 11 | 12 | {ldelim}funcname{rdelim} is how functions look in Smarty! 13 | ``` 14 | 15 | The above example will output: 16 | 17 | ``` 18 | {funcname} is how functions look in Smarty! 19 | ``` 20 | 21 | Another example with some Javascript 22 | 23 | ```smarty 24 | 29 | ``` 30 | 31 | will output 32 | 33 | ```html 34 | 39 | ``` 40 | 41 | ```smarty 42 | 47 | Click here for Server Info 48 | ``` 49 | 50 | See also [`{literal}`](./language-function-literal.md) and [escaping Smarty 51 | parsing](../language-basic-syntax/language-escaping.md). 52 | -------------------------------------------------------------------------------- /docs/api/caching/custom-storage-layers.md: -------------------------------------------------------------------------------- 1 | # Custom cache storage layers 2 | 3 | As an alternative to using the default file-based caching mechanism, you 4 | can specify a custom cache implementation that will be used to read, 5 | write and clear cached files. 6 | 7 | With a custom cache implementation you could replace the slow filesystem by a 8 | faster storage engine, centralize the cache to be accessible to multiple 9 | servers. 10 | 11 | Smarty requires implementations to extend `\Smarty\Cacheresource\Base`, but encourages you to either extend 12 | `\Smarty\Cacheresource\Custom` or `\Smarty\Cacheresource\KeyValueStore`. 13 | 14 | - `\Smarty\Cacheresource\Custom` is a simple API directing all read, write, 15 | clear calls to your implementation. This API allows you to store 16 | wherever and however you deem fit. 17 | - `\Smarty\Cacheresource\KeyValueStore` allows you to turn any 18 | KeyValue-Store (like APC or Memcache) into a full-featured 19 | CacheResource implementation. Everything around deep 20 | cache-groups like "a|b|c" is being handled for you in a way that 21 | guarantees clearing the cache-group "a" will clear all nested groups 22 | as well - even though KeyValue-Stores don't allow this kind of 23 | hierarchy by nature. 24 | 25 | Custom CacheResources must be registered on 26 | runtime with `Smarty\Smarty::setCacheResource()`: 27 | 28 | ```php 29 | setCacheResource(new My_CacheResource_Mysql()); 35 | ``` 36 | 37 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-capitalize.md: -------------------------------------------------------------------------------- 1 | # capitalize 2 | 3 | This is used to capitalize the first letter of all words in a variable. 4 | This is similar to the PHP [`ucwords()`](https://www.php.net/ucwords) 5 | function. 6 | 7 | ## Basic usage 8 | ```smarty 9 | {$myVar|capitalize} 10 | ``` 11 | 12 | ## Parameters 13 | 14 | | Parameter | Type | Required | Description | 15 | |-----------|---------|----------|-------------------------------------------------------------------------------------------------------| 16 | | 1 | boolean | No | This determines whether or not words with digits will be uppercased | 17 | | 2 | boolean | No | This determines whether or not Capital letters within words should be lowercased, e.g. "aAa" to "Aaa" | 18 | 19 | 20 | ## Examples 21 | 22 | ```php 23 | assign('articleTitle', 'next x-men film, x3, delayed.'); 26 | 27 | ``` 28 | 29 | 30 | Where the template is: 31 | 32 | ```smarty 33 | {$articleTitle} 34 | {$articleTitle|capitalize} 35 | {$articleTitle|capitalize:true} 36 | ``` 37 | 38 | 39 | Will output: 40 | 41 | ``` 42 | next x-men film, x3, delayed. 43 | Next X-Men Film, x3, Delayed. 44 | Next X-Men Film, X3, Delayed. 45 | ``` 46 | 47 | 48 | See also [`lower`](language-modifier-lower.md) and 49 | [`upper`](language-modifier-upper.md) 50 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smarty/smarty", 3 | "type": "library", 4 | "description": "Smarty - the compiling PHP template engine", 5 | "keywords": [ 6 | "templating" 7 | ], 8 | "homepage": "https://smarty-php.github.io/smarty/", 9 | "license": "LGPL-3.0", 10 | "authors": [ 11 | { 12 | "name": "Monte Ohrt", 13 | "email": "monte@ohrt.com" 14 | }, 15 | { 16 | "name": "Uwe Tews", 17 | "email": "uwe.tews@googlemail.com" 18 | }, 19 | { 20 | "name": "Rodney Rehm", 21 | "email": "rodney.rehm@medialize.de" 22 | }, 23 | { 24 | "name": "Simon Wisselink", 25 | "homepage": "https://www.iwink.nl/" 26 | } 27 | ], 28 | "support": { 29 | "issues": "https://github.com/smarty-php/smarty/issues", 30 | "forum": "https://github.com/smarty-php/smarty/discussions" 31 | }, 32 | "require": { 33 | "php": "^7.2 || ^8.0", 34 | "symfony/polyfill-mbstring": "^1.27" 35 | }, 36 | "autoload": { 37 | "psr-4" : { 38 | "Smarty\\" : "src/" 39 | }, 40 | "files": [ 41 | "src/functions.php" 42 | ] 43 | }, 44 | "extra": { 45 | "branch-alias": { 46 | "dev-master": "5.0.x-dev" 47 | } 48 | }, 49 | "require-dev": { 50 | "phpunit/phpunit": "^8.5 || ^7.5", 51 | "smarty/smarty-lexer": "^4.0.2" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-register-class.md: -------------------------------------------------------------------------------- 1 | registerClass() 2 | 3 | register a class for use in the templates 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | registerClass 11 | 12 | string 13 | 14 | class\_name 15 | 16 | string 17 | 18 | class\_impl 19 | 20 | Smarty allows you to access static classes from templates as long as the 21 | [Security Policy](#advanced.features.security) does not tell it 22 | otherwise. If security is enabled, classes registered with 23 | `registerClass()` are accessible to templates. 24 | 25 | 26 | registerClass("Foo", "Bar"); 35 | 36 | 37 | 38 | 39 | {* Smarty will access this class as long as it's not prohibited by security *} 40 | {Bar::$property} 41 | {* Foo translates to the real class Bar *} 42 | {Foo::$property} 43 | 44 | 45 | 46 | 47 | registerClass("Foo", "\my\php\application\Bar"); 58 | 59 | 60 | 61 | 62 | {* Foo translates to the real class \my\php\application\Bar *} 63 | {Foo::$property} 64 | 65 | 66 | 67 | See also [`registerObject()`](#api.register.object), and 68 | [Security](#advanced.features.security). 69 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-setfilter.md: -------------------------------------------------------------------------------- 1 | # {setfilter} 2 | 3 | The `{setfilter}...{/setfilter}` block tag allows the definition of 4 | template instance's variable filters. 5 | 6 | SYNTAX: `{setfilter filter1\|filter2\|filter3\....}\...{/setfilter}` 7 | 8 | The filter can be: 9 | 10 | - A variable filter plugin specified by it's name. 11 | 12 | - A modifier specified by it's name and optional additional 13 | parameter. 14 | 15 | `{setfilter}...{/setfilter}` blocks can be nested. The filter definition 16 | of inner blocks does replace the definition of the outer block. 17 | 18 | Template instance filters run in addition to other modifiers and 19 | filters. They run in the following order: modifier, default_modifier, 20 | $escape_html, registered variable filters, autoloaded variable 21 | filters, template instance's variable filters. Everything after 22 | default_modifier can be disabled with the `nofilter` flag. 23 | 24 | > **Note** 25 | > 26 | > The setting of template instance filters does not affect the output of 27 | > included subtemplates. 28 | 29 | ## Examples 30 | 31 | ```smarty 32 | 42 | ``` 43 | 44 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/language-function-extends.md: -------------------------------------------------------------------------------- 1 | # {extends} 2 | 3 | `{extends}` tags are used in child templates in template inheritance for 4 | extending parent templates. For details see section of [Template 5 | Inheritance](../../api/inheritance.md). 6 | 7 | - The `{extends}` tag must be on the first line of the template. 8 | 9 | - If a child template extends a parent template with the `{extends}` 10 | tag it may contain only `{block}` tags. Any other template content 11 | is ignored. 12 | 13 | - Use the syntax for [template resources](../../api/resources.md) to extend files 14 | outside the [`$template_dir`](../../programmers/api-variables/variable-template-dir.md) directory. 15 | 16 | ## Attributes 17 | 18 | | Attribute | Required | Description | 19 | |-----------|----------|-------------------------------------------------| 20 | | file | Yes | The name of the template file which is extended | 21 | 22 | > **Note** 23 | > 24 | > When extending a variable parent like `{extends file=$parent_file}`, 25 | > make sure you include `$parent_file` in the 26 | > [`$compile_id`](../../programmers/api-variables/variable-compile-id.md). Otherwise, Smarty cannot 27 | > distinguish between different `$parent_file`s. 28 | 29 | ## Examples 30 | 31 | ```smarty 32 | {extends file='parent.tpl'} 33 | {extends 'parent.tpl'} {* short-hand *} 34 | ``` 35 | 36 | See also [Template Inheritance](../../api/inheritance.md) 37 | and [`{block}`](./language-function-block.md). 38 | -------------------------------------------------------------------------------- /docs/programmers/api-variables/variable-compile-id.md: -------------------------------------------------------------------------------- 1 | \$compile\_id {#variable.compile.id} 2 | ============= 3 | 4 | Persistent compile identifier. As an alternative to passing the same 5 | `$compile_id` to each and every function call, you can set this 6 | `$compile_id` and it will be used implicitly thereafter. 7 | 8 | If you use the same template with different [pre- and/or 9 | post-filters](#plugins.prefilters.postfilters) you must use a unique 10 | `$compile_id` to keep the compiled template files separated. 11 | 12 | For example a [prefilter](#plugins.prefilters.postfilters) that 13 | localizes your templates (that is: translates language dependent parts) 14 | at compile time, then you could use the current language as 15 | `$compile_id` and you will get a set of compiled templates for each 16 | language you use. 17 | 18 | 19 | compile_id = 'en'; 21 | ?> 22 | 23 | 24 | 25 | Another application would be to use the same compile directory across 26 | multiple domains / multiple virtual hosts. 27 | 28 | 29 | compile_id = $_SERVER['SERVER_NAME']; 32 | $smarty->compile_dir = '/path/to/shared_compile_dir'; 33 | 34 | ?> 35 | 36 | 37 | 38 | > **Note** 39 | > 40 | > In Smarty 3 a `$compile_id` is no longer required to keep templates 41 | > with same name in different [`$template_dir` 42 | > folders](#variable.template.dir) separated. The [`$template_dir` file 43 | > path](#variable.template.dir) is encoded in the file name of compiled 44 | > and cached template files. 45 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-clear-cache.md: -------------------------------------------------------------------------------- 1 | clearCache() 2 | 3 | clears the cache for a specific template 4 | 5 | Description 6 | =========== 7 | 8 | void 9 | 10 | clearCache 11 | 12 | string 13 | 14 | template 15 | 16 | string 17 | 18 | cache\_id 19 | 20 | string 21 | 22 | compile\_id 23 | 24 | int 25 | 26 | expire\_time 27 | 28 | - If you have [multiple caches](#caching.multiple.caches) for a 29 | template, you can clear a specific cache by supplying the `cache_id` 30 | as the second parameter. 31 | 32 | - You can also pass a [`$compile_id`](#variable.compile.id) as a third 33 | parameter. You can [group templates together](#caching.groups) so 34 | they can be removed as a group, see the [caching section](#caching) 35 | for more information. 36 | 37 | - As an optional fourth parameter, you can supply a minimum age in 38 | seconds the cache file must be before it will get cleared. 39 | 40 | > **Note** 41 | > 42 | > Since Smarty version 3.1.14 it is possible to delete cache files 43 | > by their individual expiration time at creation by passing 44 | > constant SMARTY::CLEAR\_EXPIRED as fourth parameter. 45 | 46 | 47 | 48 | 49 | clearCache('index.tpl'); 52 | 53 | // clear the cache for a particular cache id in an multiple-cache template 54 | $smarty->clearCache('index.tpl', 'MY_CACHE_ID'); 55 | ?> 56 | 57 | 58 | 59 | See also [`clearAllCache()`](#api.clear.all.cache) and 60 | [`caching`](#caching) section. 61 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Smarty Documentation 2 | Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. 3 | 4 | It allows you to write **templates**, using **variables**, **modifiers**, **functions** and **comments**, like this: 5 | ```smarty 6 |

{$title|escape}

7 | 8 |

9 | The number of pixels is: {math equation="x * y" x=$height y=$width}. 10 |

11 | ``` 12 | 13 | When this template is rendered, with the value "Hello world" for the variable $title, 640 for $width, 14 | and 480 for $height, the result is: 15 | ```html 16 |

Hello world

17 | 18 |

19 | The number of pixels is: 307200. 20 |

21 | ``` 22 | 23 | ## Getting Started 24 | - [Getting Started](./getting-started.md) 25 | - [Philosophy](./philosophy.md) - or "Why do I need a template engine?" 26 | - [Features](./features.md) - or "Why do I want Smarty?" 27 | 28 | ## Help 29 | - [Search or create an issue](https://github.com/smarty-php/smarty/issues) 30 | - [Upgrading from an older version](upgrading.md) 31 | - [Some random tips & tricks](./appendixes/tips.md) 32 | - [Troubleshooting](./appendixes/troubleshooting.md) 33 | 34 | ## Source code 35 | - [Smarty repository at GitHub](https://github.com/smarty-php/smarty) 36 | 37 | ## Sponsors 38 | 39 | ### iWink logo 40 | 41 | [iWink](https://www.iwink.nl/), fullservice digital agency 42 | 43 | ### Temma logo 44 | 45 | [Temma](https://www.temma.net/), the MVC framework based on Smarty 46 | -------------------------------------------------------------------------------- /src/Compile/DefaultHandlerFunctionCallCompiler.php: -------------------------------------------------------------------------------- 1 | getAttributes($compiler, $args); 34 | unset($_attr['nocache']); 35 | 36 | $_paramsArray = $this->formatParamsArray($_attr); 37 | $_params = 'array(' . implode(',', $_paramsArray) . ')'; 38 | 39 | $output = "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" . var_export($function, true) . 40 | ",'function')($_params, \$_smarty_tpl)"; 41 | 42 | if (!empty($parameter['modifierlist'])) { 43 | $output = $compiler->compileModifier($parameter['modifierlist'], $output); 44 | } 45 | return "\n"; 46 | } 47 | } -------------------------------------------------------------------------------- /docs/img/smarty.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Compile/Tag/ForeachClose.php: -------------------------------------------------------------------------------- 1 | loopNesting--; 35 | 36 | [$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach', 'foreachelse']); 37 | 38 | if ($nocache_pushed) { 39 | // pop the pushed virtual nocache tag 40 | $this->closeTag($compiler, 'nocache'); 41 | $compiler->tag_nocache = true; 42 | } 43 | 44 | $output = "setVariable('{$item}', {$localVariablePrefix}Backup);\n"; 47 | } 48 | $output .= "}\n"; 49 | /* @var \Smarty\Compile\Tag\ForeachTag $foreachCompiler */ 50 | $foreachCompiler = $compiler->getTagCompiler('foreach'); 51 | $output .= $foreachCompiler->compileRestore(1); 52 | $output .= "?>"; 53 | return $output; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Compile/Modifier/Base.php: -------------------------------------------------------------------------------- 1 | **Note** 15 | > 16 | > `{strip}{/strip}` does not affect the contents of template variables, 17 | > see the [strip modifier](../language-modifiers/language-modifier-strip.md) instead. 18 | 19 | ```smarty 20 | {* the following will be all run into one line upon output *} 21 | {strip} 22 | 23 | 24 | 29 | 30 |
25 | 26 | This is a test 27 | 28 |
31 | {/strip} 32 | ``` 33 | 34 | The above example will output: 35 | 36 | ```html 37 |
This is a test
38 | ``` 39 | 40 | Notice that in the above example, all the lines begin and end with HTML 41 | tags. Be aware that all the lines are run together. If you have plain 42 | text at the beginning or end of any line, they will be run together, and 43 | may not be desired results. 44 | 45 | See also the [`strip`](../language-modifiers/language-modifier-strip.md) modifier. 46 | -------------------------------------------------------------------------------- /docs/designers/language-basic-syntax/language-syntax-tags.md: -------------------------------------------------------------------------------- 1 | # Tags 2 | 3 | Every Smarty tag either prints a [variable](./language-syntax-variables.md) or 4 | invokes some sort of function. These are processed and displayed by 5 | enclosing the function and its [attributes](./language-syntax-attributes.md) 6 | within delimiters like so: `{funcname attr1="val1" attr2="val2"}`. 7 | 8 | ## Examples 9 | 10 | ```smarty 11 | {config_load file="colors.conf"} 12 | 13 | {include file="header.tpl"} 14 | 15 | {if $logged_in} 16 | Welcome, {$name}! 17 | {else} 18 | hi, {$name} 19 | {/if} 20 | 21 | {include file="footer.tpl"} 22 | ``` 23 | 24 | - Both [built-in functions](../language-builtin-functions/index.md) and [custom 25 | functions](../language-custom-functions/index.md) have the same syntax within 26 | templates. 27 | 28 | - Built-in functions are the **inner** workings of Smarty, such as 29 | [`{if}`](../language-builtin-functions/language-function-if.md), 30 | [`{section}`](../language-builtin-functions/language-function-section.md) and 31 | [`{strip}`](../language-builtin-functions/language-function-strip.md). There should be no need to 32 | change or modify them. 33 | 34 | - Custom tags are **additional** tags implemented via 35 | [plugins](../../api/extending/introduction.md). They can be modified to your liking, or you can 36 | create new ones. [`{html_options}`](../language-custom-functions/language-function-html-options.md) 37 | is an example of a custom function. 38 | 39 | See also [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md) 40 | -------------------------------------------------------------------------------- /libs/Smarty.class.php: -------------------------------------------------------------------------------- 1 | testInstall(); // 14 | ///////////////////////////////////////////////////////////////////// 15 | 16 | define('__SMARTY_DIR', __DIR__ . '/../src/'); 17 | 18 | // Global function declarations 19 | require_once(__SMARTY_DIR . "/functions.php"); 20 | 21 | spl_autoload_register(function ($class) { 22 | // Class prefix 23 | $prefix = 'Smarty\\'; 24 | 25 | // Does the class use the namespace prefix? 26 | $len = strlen($prefix); 27 | if (strncmp($prefix, $class, $len) !== 0) { 28 | // If not, move to the next registered autoloader 29 | return; 30 | } 31 | 32 | // Hack off the prefix part 33 | $relative_class = substr($class, $len); 34 | 35 | // Build a path to the include file 36 | $file = __SMARTY_DIR . str_replace('\\', '/', $relative_class) . '.php'; 37 | 38 | // If the file exists, require it 39 | if (file_exists($file)) { 40 | require_once($file); 41 | } 42 | }); 43 | -------------------------------------------------------------------------------- /docs/designers/language-modifiers/language-modifier-regex-replace.md: -------------------------------------------------------------------------------- 1 | # regex_replace 2 | 3 | A regular expression search and replace on a variable. Use the 4 | [`preg_replace()`](https://www.php.net/preg_replace) syntax from the PHP 5 | manual. 6 | 7 | ## Basic usage 8 | ```smarty 9 | {$myVar|regex_replace:"/foo/":"bar"} 10 | ``` 11 | 12 | > **Note** 13 | > 14 | > Although Smarty supplies this regex convenience modifier, it is 15 | > usually better to apply regular expressions in PHP, either via custom 16 | > functions or modifiers. Regular expressions are considered application 17 | > code and are not part of presentation logic. 18 | 19 | ## Parameters 20 | 21 | | Parameter Position | Type | Required | Description | 22 | |--------------------|--------|----------|------------------------------------------------| 23 | | 1 | string | Yes | This is the regular expression to be replaced. | 24 | | 2 | string | Yes | This is the string of text to replace with. | 25 | 26 | 27 | ## Examples 28 | 29 | ```php 30 | assign('articleTitle', "Infertility unlikely to\nbe passed on, experts say."); 33 | 34 | ``` 35 | 36 | Where template is: 37 | 38 | ```smarty 39 | {* replace each carriage return, tab and new line with a space *} 40 | 41 | {$articleTitle} 42 | {$articleTitle|regex_replace:"/[\r\t\n]/":" "} 43 | ``` 44 | 45 | Will output: 46 | 47 | ``` 48 | Infertility unlikely to 49 | be passed on, experts say. 50 | Infertility unlikely to be passed on, experts say. 51 | ``` 52 | 53 | 54 | See also [`replace`](language-modifier-replace.md) and 55 | [`escape`](language-modifier-escape.md). 56 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-create-template.md: -------------------------------------------------------------------------------- 1 | createTemplate() 2 | 3 | returns a template object 4 | 5 | Description 6 | =========== 7 | 8 | Smarty\_Internal\_Template 9 | 10 | createTemplate 11 | 12 | string 13 | 14 | template 15 | 16 | object 17 | 18 | parent 19 | 20 | Smarty\_Internal\_Template 21 | 22 | createTemplate 23 | 24 | string 25 | 26 | template 27 | 28 | array 29 | 30 | data 31 | 32 | Smarty\_Internal\_Template 33 | 34 | createTemplate 35 | 36 | string 37 | 38 | template 39 | 40 | string 41 | 42 | cache\_id 43 | 44 | string 45 | 46 | compile\_id 47 | 48 | object 49 | 50 | parent 51 | 52 | Smarty\_Internal\_Template 53 | 54 | createTemplate 55 | 56 | string 57 | 58 | template 59 | 60 | string 61 | 62 | cache\_id 63 | 64 | string 65 | 66 | compile\_id 67 | 68 | array 69 | 70 | data 71 | 72 | This creates a template object which later can be rendered by the 73 | [display](#api.display) or [fetch](#api.fetch) method. It uses the 74 | following parameters: 75 | 76 | - `template` must be a valid [template resource](#resources) type and 77 | path. 78 | 79 | 80 | 81 | 82 | createTemplate('index.tpl'); 88 | 89 | // assign variable to template scope 90 | $tpl->assign('foo','bar'); 91 | 92 | // display the template 93 | $tpl->display(); 94 | ?> 95 | 96 | 97 | 98 | See also [`display()`](#api.display), and 99 | [`templateExists()`](#api.template.exists). 100 | -------------------------------------------------------------------------------- /docs/designers/language-custom-functions/language-function-counter.md: -------------------------------------------------------------------------------- 1 | # {counter} 2 | 3 | `{counter}` is used to print out a count. `{counter}` will remember the 4 | count on each iteration. You can adjust the number, the interval and the 5 | direction of the count, as well as determine whether to print the 6 | value. You can run multiple counters concurrently by supplying a unique 7 | name for each one. If you do not supply a name, the name "default" will 8 | be used. 9 | 10 | ## Attributes 11 | 12 | | Attribute Name | Required | Description | 13 | |----------------|----------|-----------------------------------------------------------| 14 | | name | No | The name of the counter | 15 | | start | No | The initial number to start counting from (defaults to 1) | 16 | | skip | No | The interval to count by (defaults to 1) | 17 | | direction | No | The direction to count (up/down) (defaults to 'up') | 18 | | print | No | Whether or not to print the value (defaults to true) | 19 | | assign | No | the template variable the output will be assigned to | 20 | 21 | If you supply the `assign` attribute, the output of the `{counter}` 22 | function will be assigned to this template variable instead of being 23 | output to the template. 24 | 25 | ## Examples 26 | 27 | ```smarty 28 | 29 | {* initialize the count *} 30 | {counter start=0 skip=2}
31 | {counter}
32 | {counter}
33 | {counter}
34 | 35 | ``` 36 | 37 | this will output: 38 | 39 | ```html 40 | 0
41 | 2
42 | 4
43 | 6
44 | ``` 45 | 46 | -------------------------------------------------------------------------------- /docs/designers/language-builtin-functions/index.md: -------------------------------------------------------------------------------- 1 | # Built-in Functions 2 | 3 | Smarty comes with several built-in functions. These built-in functions 4 | are the integral part of the smarty template engine. They are compiled 5 | into corresponding inline PHP code for maximum performance. 6 | 7 | You cannot create your own [custom tags](../language-custom-functions/index.md) with the same name; and you 8 | should not need to modify the built-in functions. 9 | 10 | A few of these functions have an `assign` attribute which collects the 11 | result the function to a named template variable instead of being 12 | output; much like the [`{assign}`](language-function-assign.md) function. 13 | 14 | - [{append}](language-function-append.md) 15 | - [{assign} or {$var=...}](language-function-assign.md) 16 | - [{block}](language-function-block.md) 17 | - [{call}](language-function-call.md) 18 | - [{capture}](language-function-capture.md) 19 | - [{config_load}](language-function-config-load.md) 20 | - [{debug}](language-function-debug.md) 21 | - [{extends}](language-function-extends.md) 22 | - [{for}](language-function-for.md) 23 | - [{foreach}, {foreachelse}](language-function-foreach.md) 24 | - [{function}](language-function-function.md) 25 | - [{if}, {elseif}, {else}](language-function-if.md) 26 | - [{include}](language-function-include.md) 27 | - [{insert}](language-function-insert.md) 28 | - [{ldelim}, {rdelim}](language-function-ldelim.md) 29 | - [{literal}](language-function-literal.md) 30 | - [{nocache}](language-function-nocache.md) 31 | - [{section}, {sectionelse}](language-function-section.md) 32 | - [{setfilter}](language-function-setfilter.md) 33 | - [{strip}](language-function-strip.md) 34 | - [{while}](language-function-while.md) 35 | 36 | -------------------------------------------------------------------------------- /src/Compile/Tag/Append.php: -------------------------------------------------------------------------------- 1 | getAttributes($compiler, $args); 43 | 44 | // map to compile assign attributes 45 | if (isset($_attr[ 'index' ])) { 46 | $_params[ 'smarty_internal_index' ] = '[' . $_attr[ 'index' ] . ']'; 47 | unset($_attr[ 'index' ]); 48 | } else { 49 | $_params[ 'smarty_internal_index' ] = '[]'; 50 | } 51 | $_new_attr = array(); 52 | foreach ($_attr as $key => $value) { 53 | $_new_attr[] = array($key => $value); 54 | } 55 | // call compile assign 56 | return parent::compile($_new_attr, $compiler, $_params); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Runtime/DefaultPluginHandlerRuntime.php: -------------------------------------------------------------------------------- 1 | defaultPluginHandler = $defaultPluginHandler; 16 | } 17 | 18 | public function hasPlugin($tag, $plugin_type): bool { 19 | if ($this->defaultPluginHandler === null) { 20 | return false; 21 | } 22 | 23 | $callback = null; 24 | 25 | // these are not used here 26 | $script = null; 27 | $cacheable = null; 28 | 29 | return (\call_user_func_array( 30 | $this->defaultPluginHandler, 31 | [ 32 | $tag, 33 | $plugin_type, 34 | null, // This used to pass $this->template, but this parameter has been removed in 5.0 35 | &$callback, 36 | &$script, 37 | &$cacheable, 38 | ] 39 | ) && $callback); 40 | } 41 | 42 | /** 43 | * @throws Exception 44 | */ 45 | public function getCallback($tag, $plugin_type) { 46 | 47 | if ($this->defaultPluginHandler === null) { 48 | return false; 49 | } 50 | 51 | $callback = null; 52 | 53 | // these are not used here 54 | $script = null; 55 | $cacheable = null; 56 | 57 | if (\call_user_func_array( 58 | $this->defaultPluginHandler, 59 | [ 60 | $tag, 61 | $plugin_type, 62 | null, // This used to pass $this->template, but this parameter has been removed in 5.0 63 | &$callback, 64 | &$script, 65 | &$cacheable, 66 | ] 67 | ) && $callback) { 68 | return $callback; 69 | } 70 | throw new Exception("Default plugin handler: Returned callback for '{$tag}' not callable at runtime"); 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /src/CompilerException.php: -------------------------------------------------------------------------------- 1 | file = $filename; 33 | } 34 | if ($line) { 35 | $this->line = $line; 36 | } 37 | } 38 | 39 | /** 40 | * @return string 41 | */ 42 | public function __toString() { 43 | return ' --> Smarty Compiler: ' . $this->message . ' <-- '; 44 | } 45 | 46 | /** 47 | * @param int $line 48 | */ 49 | public function setLine($line) { 50 | $this->line = $line; 51 | } 52 | 53 | /** 54 | * The template source snippet relating to the error 55 | * 56 | * @type string|null 57 | */ 58 | public $source = null; 59 | 60 | /** 61 | * The raw text of the error message 62 | * 63 | * @type string|null 64 | */ 65 | public $desc = null; 66 | 67 | /** 68 | * The resource identifier or template name 69 | * 70 | * @type string|null 71 | */ 72 | public $template = null; 73 | } 74 | -------------------------------------------------------------------------------- /docs/programmers/api-functions/api-compile-all-templates.md: -------------------------------------------------------------------------------- 1 | compileAllTemplates() 2 | 3 | compiles all known templates 4 | 5 | Description 6 | =========== 7 | 8 | string 9 | 10 | compileAllTemplates 11 | 12 | string 13 | 14 | extension 15 | 16 | boolean 17 | 18 | force 19 | 20 | integer 21 | 22 | timelimit 23 | 24 | integer 25 | 26 | maxerror 27 | 28 | This function compiles template files found in the 29 | [`$template_dir`](#variable.template.dir) folder. It uses the following 30 | parameters: 31 | 32 | - `extension` is an optional string which defines the file extension 33 | for the template files. The default is \".tpl\". 34 | 35 | - `force` is an optional boolean which controls if only modified 36 | (false) or all (true) templates shall be compiled. The default is 37 | \"false\". 38 | 39 | - `timelimit` is an optional integer to set a runtime limit in seconds 40 | for the compilation process. The default is no limit. 41 | 42 | - `maxerror` is an optional integer to set an error limit. If more 43 | templates failed to compile the function will be aborted. The 44 | default is no limit. 45 | 46 | > **Note** 47 | > 48 | > This function may not create desired results in all configurations. 49 | > Use is on own risk. 50 | 51 | > **Note** 52 | > 53 | > If any template requires registered plugins, filters or objects you 54 | > must register all of them before running this function. 55 | 56 | > **Note** 57 | > 58 | > If you are using template inheritance this function will create 59 | > compiled files of parent templates which will never be used. 60 | 61 | 62 | compileAllTemplates('.tpl',true); 68 | 69 | ?> 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/FunctionHandler/Counter.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class Counter extends Base { 20 | 21 | private $counters = []; 22 | 23 | public function handle($params, Template $template) { 24 | $name = (isset($params['name'])) ? $params['name'] : 'default'; 25 | if (!isset($this->counters[$name])) { 26 | $this->counters[$name] = ['start' => 1, 'skip' => 1, 'direction' => 'up', 'count' => 1]; 27 | } 28 | $counter =& $this->counters[$name]; 29 | if (isset($params['start'])) { 30 | $counter['start'] = $counter['count'] = (int)$params['start']; 31 | } 32 | if (!empty($params['assign'])) { 33 | $counter['assign'] = $params['assign']; 34 | } 35 | if (isset($counter['assign'])) { 36 | $template->assign($counter['assign'], $counter['count']); 37 | } 38 | if (isset($params['print'])) { 39 | $print = (bool)$params['print']; 40 | } else { 41 | $print = empty($counter['assign']); 42 | } 43 | if ($print) { 44 | $retval = $counter['count']; 45 | } else { 46 | $retval = null; 47 | } 48 | if (isset($params['skip'])) { 49 | $counter['skip'] = $params['skip']; 50 | } 51 | if (isset($params['direction'])) { 52 | $counter['direction'] = $params['direction']; 53 | } 54 | if ($counter['direction'] === 'down') { 55 | $counter['count'] -= $counter['skip']; 56 | } else { 57 | $counter['count'] += $counter['skip']; 58 | } 59 | return $retval; 60 | } 61 | } -------------------------------------------------------------------------------- /src/Compile/Tag/Inheritance.php: -------------------------------------------------------------------------------- 1 | prefixCompiledCode .= "getInheritance()->init(\$_smarty_tpl, " . 33 | var_export($initChildSequence, true) . ");\n?>\n"; 34 | } 35 | 36 | /** 37 | * Register post compile callback to compile inheritance initialization code 38 | * 39 | * @param \Smarty\Compiler\Template $compiler 40 | * @param bool|false $initChildSequence if true force child template 41 | */ 42 | public function registerInit(\Smarty\Compiler\Template $compiler, $initChildSequence = false) 43 | { 44 | if ($initChildSequence || !isset($compiler->_cache[ 'inheritanceInit' ])) { 45 | $compiler->registerPostCompileCallback( 46 | array(self::class, 'postCompile'), 47 | array($initChildSequence), 48 | 'inheritanceInit', 49 | $initChildSequence 50 | ); 51 | $compiler->_cache[ 'inheritanceInit' ] = true; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Compile/Tag/ConfigLoad.php: -------------------------------------------------------------------------------- 1 | getAttributes($compiler, $args); 69 | 70 | // save possible attributes 71 | $conf_file = $_attr['file']; 72 | $section = $_attr['section'] ?? 'null'; 73 | 74 | // create config object 75 | return "configLoad({$conf_file}, {$section});\n?>\n"; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Resource/StreamPlugin.php: -------------------------------------------------------------------------------- 1 | uid = false; 38 | $source->content = $this->getContent($source); 39 | $source->timestamp = $source->exists = !!$source->content; 40 | } 41 | 42 | /** 43 | * Load template's source from stream into current template object 44 | * 45 | * @param Source $source source object 46 | * 47 | * @return string template source 48 | */ 49 | public function getContent(Source $source) { 50 | 51 | if (strpos($source->getResourceName(), '://') !== false) { 52 | $filepath = $source->getResourceName(); 53 | } else { 54 | $filepath = str_replace(':', '://', $source->getFullResourceName()); 55 | } 56 | 57 | $t = ''; 58 | // the availability of the stream has already been checked in Smarty\Resource\Base::fetch() 59 | $fp = fopen($filepath, 'r+'); 60 | if ($fp) { 61 | while (!feof($fp) && ($current_line = fgets($fp)) !== false) { 62 | $t .= $current_line; 63 | } 64 | fclose($fp); 65 | return $t; 66 | } else { 67 | return false; 68 | } 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /docs/designers/language-basic-syntax/language-syntax-attributes.md: -------------------------------------------------------------------------------- 1 | # Attributes 2 | 3 | Most of the [tags](./language-syntax-tags.md) take attributes that 4 | specify or modify their behavior. Attributes to Smarty functions are 5 | much like HTML attributes. Static values don't have to be enclosed in 6 | quotes, but it is required for literal strings. Variables with or 7 | without modifiers may also be used, and should not be in quotes. You can 8 | even use PHP function results, plugin results and complex expressions. 9 | 10 | Some attributes require boolean values (TRUE or FALSE). These can be 11 | specified as `true` and `false`. If an attribute has no value assigned 12 | it gets the default boolean value of true. 13 | 14 | ## Examples 15 | ```smarty 16 | {include file="header.tpl"} 17 | 18 | {include file="header.tpl" nocache} // is equivalent to nocache=true 19 | 20 | {include file="header.tpl" attrib_name="attrib value"} 21 | 22 | {include file=$includeFile} 23 | 24 | {include file=#includeFile# title="My Title"} 25 | 26 | {assign var=foo value={counter}} // plugin result 27 | 28 | {assign var=foo value=substr($bar,2,5)} // PHP function result 29 | 30 | {assign var=foo value=$bar|strlen} // using modifier 31 | 32 | {assign var=foo value=$buh+$bar|strlen} // more complex expression 33 | 34 | {html_select_date display_days=true} 35 | 36 | {mailto address="smarty@example.com"} 37 | 38 | 41 | ``` 42 | 43 | > **Note** 44 | > 45 | > Although Smarty can handle some very complex expressions and syntax, 46 | > it is a good rule of thumb to keep the template syntax minimal and 47 | > focused on presentation. If you find your template syntax getting too 48 | > complex, it may be a good idea to move the bits that do not deal 49 | > explicitly with presentation to PHP by way of plugins or modifiers. 50 | --------------------------------------------------------------------------------