I need to create a series of templates that output a JSON string for Ajax to consume and utilize in the frontend. My other templates could benefit from using this as it too needs the same data. Is there a way for a template to embed a template that outputs a JSON string and somehow use it (e.g. assign it to a variable, loop through it, access each index, etc.)?
3 Answers
Take a look at JSON by Rob Sanchez. It works very well if you need to output ExpressionEngine data in JSON format. It has a lot of useful parameters.
The plugin tag can go right into your template where you want your JSON object.
<script>
var json = {exp:json:entries channel="programs"};
console.log(json);
</script>
Or you could put it in its own template and then load it with jQuery $.ajax()
.
-
This is the best solution for a quick implementation with a slight learning curve. Commented Jun 13, 2014 at 16:56
-
This looks like an easy way to output JSON. However, my issue is how to consume this JSON from other templates and use them there. Suggestions? Also, what do I do with this addon to output JSONP; possible? Commented Jun 13, 2014 at 23:57
-
Yes, take a look at
jsonp="yes"
parameter in the docs github.com/rsanchez/json#global-parameters. If you want to use this JSON in other templates then put it in its own template and load with jQuery $.ajax– Alex Kendrick ♦Commented Jun 15, 2014 at 22:53 -
I'm not sure if I understand your approach about templates consuming templates that output JSON. I want to be able to
{embed}
these templates, then use the data in that template. This happens on the server side, so I am confused by your suggestion to use jQuery. Can you please elaborate? Commented Jun 15, 2014 at 23:45 -
I understood your question to be asking how to create JSON with EE data. I didn't read your question very carefully, sorry. Still, if I understand your question, it might help to reword it as, "I have some JSON, how do I parse it on the server side with EE?"– Alex Kendrick ♦Commented Jun 16, 2014 at 14:11
For this you gonna need an add-on. EECMS can't do this by its own.
There are two good options:
- Rest - it's a good option. Besides the developer, who has an excellent reputation, had abandoned the project, he continues to accept pull requests and the add-on is used by a lot of developers;
- AJW Feed Parser - I usually prefer this, because of its caching functionality. It caches the source, so you can use the data on several pages without to fetch them again until the cache expires.
Both of them treat the source as an external one.
One answer is to create a new template and create your own custom implementation of JSON.This may be way easier than having to learn a huge api for something simple.
//JSON STRING
{entries:
[
{exp:channel:entries}
{entry_title:{title},entry_content:{content}},
{/exp:channel:entries}
]
}
The way you would implement this in JavaScript would be to parse the returned AJAX string.
//EXAMPLE
myJSONObject.entries[0].entry_title;
To make this code multipurpose use if/esle statements and pass variables to the template when you embed it.
For instance,
{if embed:isEmbedded == "yes"}
//Javascript for embedded templates
var myJSONObject =
{/if}
//JSON STRING
{entries:
[
{exp:channel:entries}
{entry_title:{title},entry_content:{content}},
{/exp:channel:entries}
]
}
In your template, the code would like this:
<script type="text/javascript">
{embed="embeds/jsonApi" isEmbedded="yes"}
//Access the JSON object
myJSONObject.entries[0].entry_title;
</script>
-
Creating the JSON is easy. The approach I was going to take was to enable PHP, loop through the Channel entries, store the values in an array, convert the array to JSON, set headers, echo the JSON (or JSONP, if needed). Templates that output JSON is the easy part. The issue I struggle with is how to embed this output in another template and use it there? Commented Jun 13, 2014 at 23:54
-
That should be simple. If part of the template is specifically for your custom JSON API and part of it for embedding in other templates just pass a variable to the template when you embed it and set up with if/else statements. Commented Jun 16, 2014 at 14:21
-
-
I still don't see how I can use the JSON in my template files. Sure, the JSON template returned a JSON string -- but how do I use it? For example, say I want to loop through it and output the title? Your template example seems to assume that it would be a JS template; what if it was an HTML template? Commented Jun 16, 2014 at 20:48
-
Using JSON in your file would require one of two methods: either use Javascript to place the data where you want it...or use PHP scripting in your template. PHP supports JSON objects. Use "json_decode()" to parse your JSON string into an object. Commented Jun 17, 2014 at 14:47