Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

{json}

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
jLix
Smarty Regular


Joined: 01 Apr 2009
Posts: 59
Location: Lowlands, EU

PostPosted: Wed Apr 01, 2009 10:20 am    Post subject: {json} Reply with quote

Hello, I've created a plugin called {json} that reads JSON data from a file (URL), decodes it and assigns it to a Smarty template variable.

It's also possible to assign only certain branches of nested arrays/objects to template variables which may be useful in cases where a single JSON file is used as some kind of configuration file for an entire project.

JSON objects can be converted to PHP objects or to associative arrays (default), using the obj2obj parameter.

Using the function:
Code:
{json file="filename.json" assign="data"}
assigns the decoded JSON data to $data, converting JSON objects to PHP associative arrays.

Code:
{json file="filename.json" assign="data" obj2obj=true}
does the same, but this time JSON objects arre converted to PHP objects.


If the data passed to $data would have a property called 'languages', it is also be possible to assign only that property to a variable like this:
Code:
{json file="filename.json" lang="languages"}
$lang would now have the same value as $data.languages did in the first example (and $data->languages in the second).

And you can have more of these in one statement:
Code:
{json file="filename.json" lang="languages" coun="countries"}


More info and the plugin itself can be found at: http://jlix.net/extensions/smarty/json
_________________
http://jlix.net/extensions/smarty
Back to top
View user's profile Send private message Visit poster's website
jLix
Smarty Regular


Joined: 01 Apr 2009
Posts: 59
Location: Lowlands, EU

PostPosted: Sun Apr 12, 2009 2:25 am    Post subject: Update: {json} 1.0.1 Reply with quote

The {json} plugin is updated to version 1.0.1

Smarty doesn't work with true booleans. It uses the value 1 instead of true. Therefore identical comparison (type checking) is dropped for equality comparison for the debug and obj2obj parameters' values.

This means that as of version 1.0.1 the following example will work the way you might expect:
Code:
{json file="filename.json" assign="data" debug=$smarty.get.debug}

This will print the decoded data in the template if &debug=true is added to the URL.


The plugin can be found at: http://jlix.net/extensions/smarty/json
_________________
http://jlix.net/extensions/smarty
Back to top
View user's profile Send private message Visit poster's website
brennsuppa
Smarty Rookie


Joined: 19 May 2009
Posts: 23
Location: Austria

PostPosted: Thu Jun 04, 2009 11:51 am    Post subject: Reply with quote

i installed this plugin and tried the examples from the website but nothing happens... (August has days)

i am using php 5.2+

maybe you can help me with that
Back to top
View user's profile Send private message Visit poster's website
jLix
Smarty Regular


Joined: 01 Apr 2009
Posts: 59
Location: Lowlands, EU

PostPosted: Thu Jun 04, 2009 5:52 pm    Post subject: Reply with quote

Hmmm, that's weird.

I just downloaded a copy from my own site to test whether the file could have become corrupt, but it just seems to work as it should.

I tried to reproduce the same output you're getting: "August has days" and without any error messages (I assume). This seems to happen when the JSON object is invalid. So perhaps you've missed a character while copying. The JSON example object on my site is valid. You can validate yours on http://www.jsonlint.com/.

Cheers,
Sander
_________________
http://jlix.net/extensions/smarty
Back to top
View user's profile Send private message Visit poster's website
brennsuppa
Smarty Rookie


Joined: 19 May 2009
Posts: 23
Location: Austria

PostPosted: Wed Jun 17, 2009 8:10 am    Post subject: Reply with quote

fixed that... was just the wrong folder Wink
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Jun 18, 2009 4:42 pm    Post subject: Version awareness Reply with quote

FYI, please note that JSON library for PHP works only after php 5.2.0. If you own a server with old php version, happen to read this forum and surprise, why this does not work; upgrade Wink
Back to top
View user's profile Send private message Visit poster's website
jLix
Smarty Regular


Joined: 01 Apr 2009
Posts: 59
Location: Lowlands, EU

PostPosted: Thu Jun 18, 2009 6:17 pm    Post subject: Reply with quote

That's correct and therefor it is stated on the download page that json_decode() support is a requirement.
json_decode() is part of PHP, version 5.2.0 and up. In older versions the PECL extension can be used.
_________________
http://jlix.net/extensions/smarty
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP