diff options
| author | Simon Wisselink <wisskid@users.noreply.github.com> | 2024-02-26 14:35:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-26 14:35:19 +0100 |
| commit | 41d80b99ac4fe8d8bdd9ae370841dfd081a7669b (patch) | |
| tree | 58401f2a0c735f9ebd08212ceafe3b3108eec1be /docs/designers/language-modifiers | |
| parent | 2b0ba0eabcd0da5ee97326e81d293de03e3c6a37 (diff) | |
| download | smarty-41d80b99ac4fe8d8bdd9ae370841dfd081a7669b.tar.gz smarty-41d80b99ac4fe8d8bdd9ae370841dfd081a7669b.tar.bz2 smarty-41d80b99ac4fe8d8bdd9ae370841dfd081a7669b.zip | |
Implemented support for substr, implode and json_encode as modifiers. (#940)
* Implemented support for substr, implode and json_encode as modifiers. Fixes #939
* Added split and join in favor of explode and implode modifiers.
* Documented all available modifiers
Diffstat (limited to 'docs/designers/language-modifiers')
14 files changed, 322 insertions, 56 deletions
diff --git a/docs/designers/language-modifiers/index.md b/docs/designers/language-modifiers/index.md index 329ed958..3f52c2e7 100644 --- a/docs/designers/language-modifiers/index.md +++ b/docs/designers/language-modifiers/index.md @@ -6,34 +6,10 @@ or strings. To apply a modifier, specify the value followed by a `|` (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifier name and are separated by a `:` -(colon). Also, *all php-functions can be used as modifiers implicitly* -(more below) and modifiers can be -[combined](../language-combining-modifiers.md). +(colon). -- [capitalize](language-modifier-capitalize.md) -- [cat](language-modifier-cat.md) -- [count_characters](language-modifier-count-characters.md) -- [count_paragraphs](language-modifier-count-paragraphs.md) -- [count_sentences](language-modifier-count-sentences.md) -- [count_words](language-modifier-count-words.md) -- [date_format](language-modifier-date-format.md) -- [default](language-modifier-default.md) -- [escape](language-modifier-escape.md) -- [from_charset](language-modifier-from-charset.md) -- [indent](language-modifier-indent.md) -- [lower](language-modifier-lower.md) -- [nl2br](language-modifier-nl2br.md) -- [regex_replace](language-modifier-regex-replace.md) -- [replace](language-modifier-replace.md) -- [spacify](language-modifier-spacify.md) -- [string_format](language-modifier-string-format.md) -- [strip](language-modifier-strip.md) -- [strip_tags](language-modifier-strip-tags.md) -- [to_charset](language-modifier-to-charset.md) -- [truncate](language-modifier-truncate.md) -- [unescape](language-modifier-unescape.md) -- [upper](language-modifier-upper.md) -- [wordwrap](language-modifier-wordwrap.md) +Modifiers can be applied to any type of variables, including arrays +and objects. ## Examples @@ -65,40 +41,63 @@ These parameters follow the modifier name and are separated by a `:` {* php's count *} {$myArray|@count} -{* this will uppercase and truncate the whole array *} +{* this will uppercase the whole array *} <select name="name_id"> -{html_options output=$my_array|upper|truncate:20} +{html_options output=$my_array|upper} </select> ``` - -- Modifiers can be applied to any type of variables, including arrays - and objects. - > **Note** - > - > The default behavior was changed with Smarty 3. In Smarty 2.x, you - > had to use an "`@`" symbol to apply a modifier to an array, such - > as `{$articleTitle|@count}`. With Smarty 3, the "`@`" is no - > longer necessary, and is ignored. - > - > If you want a modifier to apply to each individual item of an - > array, you will either need to loop the array in the template, or - > provide for this functionality inside your modifier function. +## Combining Modifiers - > **Note** - > - > Second, in Smarty 2.x, modifiers were applied to the result of - > math expressions like `{8+2}`, meaning that - > `{8+2|count_characters}` would give `2`, as 8+2=10 and 10 is two - > characters long. With Smarty 3, modifiers are applied to the - > variables or atomic expressions before executing the calculations, - > so since 2 is one character long, `{8+2|count_characters}` - > gives 9. To get the old result use parentheses like - > `{(8+2)|count_characters}`. +You can apply any number of modifiers to a variable. They will be +applied in the order they are combined, from left to right. They must be +separated with a `|` (pipe) character. -- Custom modifiers can be registered - with the [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md) - function. +```php +<?php + +$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.'); +``` + +where template is: + +```smarty +{$articleTitle} +{$articleTitle|upper|spacify} +{$articleTitle|lower|spacify|truncate} +{$articleTitle|lower|truncate:30|spacify} +{$articleTitle|lower|spacify|truncate:30:". . ."} +``` + + +The above example will output: + +``` +Smokers are Productive, but Death Cuts Efficiency. +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 . +s m o k e r s a r ....snip.... b u t d e a t h c u t s... +s m o k e r s a r e p r o d u c t i v e , b u t . . . +s m o k e r s a r e p. . . +``` + +## Using modifiers in expressions + +Modifiers can also be used in expressions. For example, you can use the [isset modifier](./language-modifier-isset.md) +to test if a variable holds a value different from null. + +```smarty +{if $varA|isset} + <b>variable A is set</b> +{/if} +``` + +You can also use modifiers in expressions in a PHP-style syntax: + +```smarty +{if isset($varA)} + <b>variable A is set</b> +{/if} +``` See also [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md), [combining modifiers](../language-combining-modifiers.md). and [extending smarty with diff --git a/docs/designers/language-modifiers/language-modifier-count.md b/docs/designers/language-modifiers/language-modifier-count.md new file mode 100644 index 00000000..36436a39 --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-count.md @@ -0,0 +1,21 @@ +# count + +Returns the number of elements in an array (or Countable object). Will return 0 for null. +Returns 1 for any other type (such as a string). + +If the optional mode parameter is set to 1, count() will recursively count the array. +This is particularly useful for counting all the elements of a multidimensional array. + +## Basic usage +```smarty +{if $myVar|count > 3}4 or more{/if} +{if count($myVar) > 3}4 or more{/if} +``` + + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|------|----------|--------------------------------------------------------| +| 1 | int | No | If set to 1, count() will recursively count the array. | + diff --git a/docs/designers/language-modifiers/language-modifier-debug-print-var.md b/docs/designers/language-modifiers/language-modifier-debug-print-var.md new file mode 100644 index 00000000..ce3f86a7 --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-debug-print-var.md @@ -0,0 +1,26 @@ +# debug_print_var + + + +Returns the value of the given variable in a human-readable format in HTML. +Used in the [debug console](../chapter-debugging-console.md), but you can also use it in your template +while developing to see what is going on under the hood. + +> **Note** +> +> Use for debugging only! Since you may accidentally reveal sensitive information or introduce vulnerabilities such as XSS using this +method never use it in production. + +## Basic usage +```smarty +{$myVar|debug_print_var} +``` + + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|------|----------|------------------------------------------------------------------------| +| 1 | int | No | maximum recursion depth if $var is an array or object (defaults to 10) | +| 2 | int | No | maximum string length if $var is a string (defaults to 40) | + diff --git a/docs/designers/language-modifiers/language-modifier-isset.md b/docs/designers/language-modifiers/language-modifier-isset.md new file mode 100644 index 00000000..83e31dfa --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-isset.md @@ -0,0 +1,11 @@ +# isset + +Returns true if the variable(s) passed to it are different from null. + +If multiple parameters are supplied then isset() will return true only if all of the parameters are +not null. + +## Basic usage +```smarty +{if $myVar|isset}all set!{/if} +``` diff --git a/docs/designers/language-modifiers/language-modifier-join.md b/docs/designers/language-modifiers/language-modifier-join.md new file mode 100644 index 00000000..9a044714 --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-join.md @@ -0,0 +1,26 @@ +# join + +Returns a string containing all the element of the given array +with the separator string between each. + +## Basic usage + +For `$myArray` populated with `['a','b','c']`, the following will return the string `abc`. +```smarty +{$myArray|join} +``` + + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|--------|----------|-------------------------------------------------------------| +| 1 | string | No | glue used between array elements. Defaults to empty string. | + +## Examples + + +For `$myArray` populated with `[1,2,3]`, the following will return the string `1-2-3`. +```smarty +{$myArray|join:"-"} +```
\ No newline at end of file diff --git a/docs/designers/language-modifiers/language-modifier-json-encode.md b/docs/designers/language-modifiers/language-modifier-json-encode.md new file mode 100644 index 00000000..4e70f0c2 --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-json-encode.md @@ -0,0 +1,27 @@ +# json_encode + +Transforms a value into a valid JSON string. + +## Basic usage +```smarty +{$user|json_encode} +``` +Depending on the value of `$user` this would return a string in JSON-format, e.g. `{"username":"my_username","email":"my_username@smarty.net"}`. + + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|------|----------|-------------------------------------------------------------------------------------------| +| 1 | int | No | bitmask of flags, directly passed to [PHP's json_encode](https://www.php.net/json_encode) | + + +## Examples + +By passing `16` as the second parameter, you can force json_encode to always format the JSON-string as an object. +Without it, an array `$myArray = ["a","b"]` would be formatted as a javascript array: + +```smarty +{$myArray|json_encode} # renders: ["a","b"] +{$myArray|json_encode:16} # renders: {"0":"a","1":"b"} +```
\ No newline at end of file diff --git a/docs/designers/language-modifiers/language-modifier-noprint.md b/docs/designers/language-modifiers/language-modifier-noprint.md new file mode 100644 index 00000000..5dbfaa30 --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-noprint.md @@ -0,0 +1,9 @@ +# noprint + +Always returns an empty string. This can be used to call a function or a method on an object that +returns output, and suppress the output. + +## Basic usage +```smarty +{$controller->sendEmail()|noprint} +``` diff --git a/docs/designers/language-modifiers/language-modifier-number-format.md b/docs/designers/language-modifiers/language-modifier-number-format.md new file mode 100644 index 00000000..44c3acf4 --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-number-format.md @@ -0,0 +1,32 @@ +# number_format + +Allows you to format a number using decimals and a thousands-separator. By default, the number of decimals is 0 +and the number is rounded. + +## Basic usage +```smarty +{$num = 2000.151} +{$num|number_format} # renders: 2,000 +``` + + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|--------|----------|---------------------------------------| +| 1 | int | No | number of decimals (defaults to 0) | +| 2 | string | No | decimal separator (defaults to ".") | +| 3 | string | No | thousands-separator (defaults to ",") | + + +## Examples + +```smarty +{$num = 2000.151} +{$num|number_format:2} # renders: 2,000.15 +``` + +```smarty +{$num = 2000.151} +{$num|number_format:2:".":""} # renders: 2000.15 +```
\ No newline at end of file diff --git a/docs/designers/language-modifiers/language-modifier-round.md b/docs/designers/language-modifiers/language-modifier-round.md new file mode 100644 index 00000000..c05b899a --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-round.md @@ -0,0 +1,35 @@ +# round + +Rounds a number to the specified precision. + +## Basic usage +```smarty +{3.14|round} # renders: 3 +``` + +```smarty +{3.141592|round:2} # renders: 3.14 +``` + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|------|----------|---------------------------| +| 1 | int | No | precision (defaults to 0) | +| 2 | int | No | mode (defaults to 1) | + +If 'precision' is negative, the number is rounded to the nearest power of 10. See examples below. + +The parameter 'mode' defines how the rounding is done. By default, 2.5 is rounded to 3, whereas 2.45 is rounded to 2. +You usually don't need to change this. For more details on rounding modes, +see [PHP's documentation on round](https://www.php.net/manual/en/function.round). + +## Examples + +By passing `16` as the second parameter, you can force json_encode to always format the JSON-string as an object. +Without it, an array `$myArray = ["a","b"]` would be formatted as a javascript array: + +```smarty +{$myArray|json_encode} # renders: ["a","b"] +{$myArray|json_encode:16} # renders: {"0":"a","1":"b"} +```
\ No newline at end of file diff --git a/docs/designers/language-modifiers/language-modifier-split.md b/docs/designers/language-modifiers/language-modifier-split.md new file mode 100644 index 00000000..caef884f --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-split.md @@ -0,0 +1,32 @@ +# split + +Splits a string into an array, using the optional second parameter as the separator. + +## Basic usage + +For `$chars` populated with `'abc'`, the following will produce a html list with 3 elements (a, b and c). +```smarty +<ol> + {foreach $chars|split as $char} + <li>{$char|escape}</li> + {/foreach} +</ol> +``` + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|--------|----------|------------------------------------------------------------------------------------------------------------------------------| +| 1 | string | No | separator used to split the string on. Defaults to empty string, causing each character in the source string to be separate. | + +## Examples + + +For `$ids` populated with `'1,2,3'`, the following will produce a html list with 3 elements (1, 2 and 3). +```smarty +<ol> + {foreach $ids|split:',' as $id} + <li>{$id|escape}</li> + {/foreach} +</ol> +```
\ No newline at end of file diff --git a/docs/designers/language-modifiers/language-modifier-str-repeat.md b/docs/designers/language-modifiers/language-modifier-str-repeat.md new file mode 100644 index 00000000..1ae1824d --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-str-repeat.md @@ -0,0 +1,14 @@ +# str_repeat + +Repeats the given value n times. + +## Basic usage +```smarty +{"hi"|str_repeat:2} # renders: hihi +``` + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|------|----------|-----------------------| +| 1 | int | yes | number of repetitions | diff --git a/docs/designers/language-modifiers/language-modifier-strlen.md b/docs/designers/language-modifiers/language-modifier-strlen.md new file mode 100644 index 00000000..4c1f37ab --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-strlen.md @@ -0,0 +1,9 @@ +# strlen + +Returns the length (number of characters) in the given string, including spaces. + +## Basic usage +```smarty +{"Smarty"|strlen} # renders: 6 +{156|strlen} # renders: 3 +``` diff --git a/docs/designers/language-modifiers/language-modifier-substr.md b/docs/designers/language-modifiers/language-modifier-substr.md new file mode 100644 index 00000000..a79d8a4d --- /dev/null +++ b/docs/designers/language-modifiers/language-modifier-substr.md @@ -0,0 +1,25 @@ +# substr + +Returns a part (substring) of the given string starting at a given offset. + +## Basic usage +```smarty +{"Smarty"|substr:2} # renders: arty +{"Smarty"|substr:2:3} # renders: art +``` + +## Parameters + +| Parameter | Type | Required | Description | +|-----------|------|----------|-----------------------------------------------------| +| 1 | int | yes | offset (zero based, can be negative) | +| 2 | int | no | length of substring returned (unlimited of omitted) | + + +## Examples + +When used with a negative offset, the substring starts n characters from the end of the string counting backwards. +```smarty +{"Smarty"|substr:-2} # renders: ty +{"Smarty"|substr:-2:1} # renders: t +```
\ No newline at end of file diff --git a/docs/designers/language-modifiers/language-modifier-upper.md b/docs/designers/language-modifiers/language-modifier-upper.md index 3173059c..edce9638 100644 --- a/docs/designers/language-modifiers/language-modifier-upper.md +++ b/docs/designers/language-modifiers/language-modifier-upper.md @@ -29,5 +29,5 @@ If Strike isn't Settled Quickly it may Last a While. IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. ``` -See also [`lower`](lower) and +See also [`lower`](./language-modifier-lower.md) and [`capitalize`](language-modifier-capitalize.md). |
