Documented all available modifiers

This commit is contained in:
Simon Wisselink
2024-02-26 12:06:29 +01:00
parent 9ef066fa85
commit 721befc194
7 changed files with 143 additions and 1 deletions

View File

@@ -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"}
```

View File

@@ -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
```

View File

@@ -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"}
```

View File

@@ -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 |

View File

@@ -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
```

View File

@@ -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
```

View File

@@ -72,7 +72,7 @@ nav:
- 'round': 'designers/language-modifiers/language-modifier-round.md'
- 'spacify': 'designers/language-modifiers/language-modifier-spacify.md'
- 'split': 'designers/language-modifiers/language-modifier-split.md'
- 'str_repeat': 'designers/language-modifiers/language-modifier-string-repeat.md'
- 'str_repeat': 'designers/language-modifiers/language-modifier-str-repeat.md'
- 'string_format': 'designers/language-modifiers/language-modifier-string-format.md'
- 'strip': 'designers/language-modifiers/language-modifier-strip.md'
- 'strip_tags': 'designers/language-modifiers/language-modifier-strip-tags.md'