Skip to content

Commit

Permalink
Init Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
diit committed Jun 17, 2015
0 parents commit acc190a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
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
32 changes: 32 additions & 0 deletions jsondecodetwigfilter/JsonDecodeTwigFilterPlugin.php
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 jsondecodetwigfilter/twigextensions/JsonDecodeTwigExtension.php
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);
}
}

0 comments on commit acc190a

Please sign in to comment.