mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-06 07:11:37 +01:00
27 lines
973 B
Markdown
27 lines
973 B
Markdown
|
|
# 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"}
|
||
|
|
```
|