Adds a json_decode filter to your craft CMS site.
Switch branches/tags
Nothing to show
Clone or download
ryanirelan and diit Added Assoc array support (#1)
* Added support for outputting an assoc array

PHP's implementation of json_decode also supports an $assoc param that allows you to output an associative array instead of an object.

* some proper formatting

* Update README.md
Latest commit e859bcb Sep 24, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
jsondecodetwigfilter Added Assoc array support (#1) Sep 24, 2017
README.md Added Assoc array support (#1) Sep 24, 2017

README.md

JSON Decode Twig Filter for Craft CMS

Adds a json_decode filter to your toolkit.

Example Use

Add extra data to fields like multiselect, in this example add icons from multiple sources.

{{ forms.multiselect({
	label: "Example Icons"|t,
	id: 'icons',
	name: 'icons',
	value: example.icons,
	errors: example.getErrors('icons'),
	required: true,
	options: [
		{
			label: 'Camping',
			value: '{ "type":"svg", "class":"camping" }'
		},{
			label: 'Adventure',
			value: '{ "type":"maki", "class":"maki-pitch" }'
		},{
			label: 'Beach',
			value: '{ "type":"maki", "class":"maki-swimming" }'
		},{
			label: 'Mountain',
			value: '{ "type":"svg", "class":"mountain" }'
		}
	]
}) }}
<div class="example__icons">
	{% for rawicon in examplePlugin.icons %}
	{% set icon = rawicon | json_decode %}
	{% if icon.type == 'maki' %}
	<span class="{{ icon.class }}"></span>
	{% elseif icon.type == 'svg' %}
	{{ SVGicons.outdoor(icon.class) }}
	{% endif %}
	{% endfor %}
</div>

Options

By default json_decode outputs an object but you can also output an associative array by passing true as a parameter value to the filter.

{% set icon = rawicon | json_decode(true)  %} 

Dale Inverarity © 2015 - All rights reserved