-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
29 changed files
with
727 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add support for implode, substr and json_encode as modifiers/functions in templates [#939](https://github.com/smarty-php/smarty/issues/939) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/designers/language-modifiers/language-modifier-count.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | | ||
|
26 changes: 26 additions & 0 deletions
26
docs/designers/language-modifiers/language-modifier-debug-print-var.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | | ||
|
11 changes: 11 additions & 0 deletions
11
docs/designers/language-modifiers/language-modifier-isset.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
``` |
26 changes: 26 additions & 0 deletions
26
docs/designers/language-modifiers/language-modifier-join.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:"-"} | ||
``` |
27 changes: 27 additions & 0 deletions
27
docs/designers/language-modifiers/language-modifier-json-encode.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} | ||
``` |
9 changes: 9 additions & 0 deletions
9
docs/designers/language-modifiers/language-modifier-noprint.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
``` |
32 changes: 32 additions & 0 deletions
32
docs/designers/language-modifiers/language-modifier-number-format.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
35 changes: 35 additions & 0 deletions
35
docs/designers/language-modifiers/language-modifier-round.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} | ||
``` |
32 changes: 32 additions & 0 deletions
32
docs/designers/language-modifiers/language-modifier-split.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
``` |
14 changes: 14 additions & 0 deletions
14
docs/designers/language-modifiers/language-modifier-str-repeat.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | |
9 changes: 9 additions & 0 deletions
9
docs/designers/language-modifiers/language-modifier-strlen.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
Oops, something went wrong.