Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added a json_decode Twig Filter. #3678

Merged
merged 3 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Added
- Added the `|json_decode` Twig filter. ([#3678](https://github.com/craftcms/cms/pull/3678))

### Fixed
- Fixed an error that occurred when updating to Craft 3.1 if a plugin or module was calling any soft-deletable records’ `find()` methods.
- Fixed an error that occurred when updating from Craft 2 to Craft 3.1 if there were any RichText fields. ([#3677](https://github.com/craftcms/cms/issues/3677))
Expand Down
8 changes: 8 additions & 0 deletions docs/dev/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ Returns an array containing only the values that are also in a passed-in array.

Like Twig’s core [json_encode](https://twig.symfony.com/doc/2.x/filters/json_encode.html) filter, but if the `options` argument isn’t set, it will default to `JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT` if the response content type is either `text/html` or `application/xhtml+xml`.

## `json_decode`

JSON-decodes a string into an array by passing it through <api:yii\helpers\Json::decode()>.

```twig
{% set arr = '[1, 2, 3]'|json_decode %}
```

## `kebab`

Returns a string formatted in “kebab-case”.
Expand Down
2 changes: 2 additions & 0 deletions src/web/twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use craft\helpers\DateTimeHelper;
use craft\helpers\Db;
use craft\helpers\FileHelper;
use craft\helpers\Json;
use craft\helpers\Sequence;
use craft\helpers\StringHelper;
use craft\helpers\Template as TemplateHelper;
Expand Down Expand Up @@ -213,6 +214,7 @@ public function getFilters(): array
new \Twig_SimpleFilter('indexOf', [$this, 'indexOfFilter']),
new \Twig_SimpleFilter('intersect', 'array_intersect'),
new \Twig_SimpleFilter('json_encode', [$this, 'jsonEncodeFilter']),
new \Twig_SimpleFilter('json_decode', [Json::class, 'json_decode']),
new \Twig_SimpleFilter('kebab', [$this, 'kebabFilter']),
new \Twig_SimpleFilter('lcfirst', [$this, 'lcfirstFilter']),
new \Twig_SimpleFilter('literal', [$this, 'literalFilter']),
Expand Down