-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit acc190a
Showing
3 changed files
with
111 additions
and
0 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,47 @@ | ||
# JSON Decode Twig Filter <small>_for Craft CMS_</small> | ||
|
||
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. | ||
|
||
```php | ||
{{ 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" }' | ||
} | ||
] | ||
}) }} | ||
``` | ||
|
||
```html | ||
<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> | ||
``` | ||
[Dale Inverarity](http://dale.wtf/) © 2015 - All rights reserved |
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 @@ | ||
<?php | ||
namespace Craft; | ||
|
||
class JsonDecodeTwigFilterPlugin extends BasePlugin { | ||
|
||
function getName() { | ||
|
||
return Craft::t('JSON Decode Twig Filter'); | ||
} | ||
|
||
function getVersion() { | ||
|
||
return '1.0.9'; | ||
} | ||
|
||
function getDeveloper() { | ||
|
||
return 'Dale Inverarity'; | ||
} | ||
|
||
function getDeveloperUrl() { | ||
|
||
return 'http://dale.wtf/'; | ||
} | ||
|
||
public function addTwigExtension() { | ||
|
||
Craft::import('plugins.JsonDecodeTwigFilterPlugin.twigextensions.JsonDecodeTwigExtension'); | ||
return new JsonDecodeTwigExtension(); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
jsondecodetwigfilter/twigextensions/JsonDecodeTwigExtension.php
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 @@ | ||
<?php | ||
namespace Craft; | ||
|
||
use Twig_Extension; | ||
use Twig_Filter_Method; | ||
|
||
class JsonDecodeTwigExtension extends \Twig_Extension { | ||
|
||
public function getName() { | ||
|
||
return 'Json Decode'; | ||
} | ||
|
||
public function getFilters() { | ||
|
||
$returnArray = array(); | ||
$methods = array( | ||
'json_decode', | ||
); | ||
|
||
foreach ($methods as $methodName) { | ||
$returnArray[$methodName] = new \Twig_Filter_Method($this, $methodName); | ||
} | ||
|
||
return $returnArray; | ||
} | ||
|
||
public function json_decode($json) { | ||
|
||
return json_decode($json); | ||
} | ||
} |