If you are looking for a quick way to assign JSON data to your WordPress page or post, the {{JSON..}} binder is the way to go. This binder allows you to define an arbitrary data source that can be bounded to you page and utilizes when rendering your page.
Motivation
While building your page template, you may want to link your design to dynamic data that you might not have at the time that you are writing the code. The {{JSON.. }} binder acts as temporary data source that later on be replaced with the dynamic data that you truly want to use.
Parameters
source
(string) (required*) The name of the internal JSON source file.
Default: none
assign_to
(string) (required*) The name of the object to which you wish to bind the data output.
Default: none
Return
This binder returns the JSON string as an object.
Basic Usage
{{json assign_to="my_info"}}
{"name": "Raul", "role": "CEO", "favorite_number": 60}
{{/json}}
<div>
<h1>{{$my_info.name}}, <i>{{$my_info.role}}</i></h1>
<small>{{$my_info.favorite_number}}</small>
</div>
Examples
Consider a scenario where you wish to show a list of “Projects“. You may choose to create a custom post type in WordPress to manage your projects, or you might prefer to create a post category to achieve the same results. Thank to the {{JSON.. }} binder you can build your template with temporary data, and later on assign the data source that fits your needs.
{{json assign_to="projects"}}
{
"data": [
{"name": "Logo Design", "client": "ACME, LLC"},
{"name": "Landing Page", "client": "Non Profit Organization"},
{"name": "Analytics Integration", "client": "Internal"}
]
}
{{/json}}
<ul>
{{foreach $projects.data as $single_project}}
<li>
<h2>{{$single_project.name}}</h2>
<p>{{$single_project.client}}</p>
</li>
{{/foreach}}
</ul>
Later on, once you determine what is the best way to manage your projects in WordPress, you can replace the {{JSON… }} binder with a different one that uses dynamic data:
{{posts show="category" name="my-projects-category-slug" assign_to="projects"}}
<ul>
{{foreach $projects.data as $single_project}}
<li>
<h2>{{$single_project.name}}</h2>
<p>{{$single_project.client}}</p>
</li>
{{/foreach}}
</ul>
Using An Internal JSON File
You can also use internal JSON files to model your project. An internal JSON file is a predefined JSON file that can be include into your project, so you do not have to create content in WordPress while building your pages.
{{json source="me" assign_to="profile"}}{{/json}}
<!-- see: http://builder.simplifysites.com/resources/data/me.js -->
<div>
<h1>{{$profile.first_name}} {{$profile.last_name}}</h1>
<p>{{$profile.website}}<br/>
{{$profile.twitter}}</p>
</div>