zesty.io

Product

Use Cases

Integrations

Learn

Parsley Header

Parsley recently underwent a large update, adding major features while supporting all previous usages. The goals of this update were threefold:

JSON as Content Iteration and Access Support

Zesty has supported outputting JSON and accepting JSON with our APIs, but we have not commonly seen JSON stored as content in the system. Now we provide Parsely support when JSON is stored as content.

JSON Content Iteration

JSON stored in a content model field can be iterated by using parsley, the same as content items.

Preview a live JSON iteration example here: https://parsley.zesty.io/iterating-over-a-content-stored-json-object-with-an-array/

<ul>
   {{each {this.json_object}.json(arr) as item}}
   <li>{{item.title}}</li>
   {{end-each}}
</ul>

Accessing Objects in JSON

Quickly access an object of an object to access its value.

Live example: https://parsley.zesty.io/accessing-a-json-object-within-an-object/

{{this.json_object.json(obj_within.name)}}

Iterating String Arrays

This refers to support for objects that store arrays of strings without keys.

Live example: https://parsley.zesty.io/iterating-through-a-json-array-of-strings/

{<ul>
{{each {this.json_object}.json(arr_strings) as item}} 
       <li>{{item._string}}</li>
{{/each}}
</ul>

Each Statement Updates

Each statement's code gets reduced with shortcut statements and easier iteration of content relationships.

Content Relationship Iteration 

Before, making content relationships in parsley required the following code to form the relationship.

Old Style

{{each tags as tag where find_in_set(z.zuid,'{article.tags}')}}

<a href="{{tag.getURL()}}">{{tag.name}}</a>

{{end-each}}

The latest update simplifies the logic and reduces code length by allowing the relationship to be directly referenced in each loop. It also provides a shortened end-each option.

New Style https://parsley.zesty.io/iterate-through-a-model-relationship-field/

{{each {article.tags} as tag}}

<a href="{{tag.getURL()}}">{{tag.name}}</a>

{{/each}}

Array Commas

Outputting custom JSON data endpoints with Parsley content syntax requires extra logic if you are outputting an array of objects. This update streamlines that logic. Here is what use to look like to output 10 random articles to JSON.

Old Style

[

{{each articles as article limit 10 order by rand() }}

   {{article.toJSON()}}

  {{if {article._num} < {article._length} }},{{end-if}}

{{end-each}}

]

Note the logic does not have a trailing comma. That is what we replace with _arraycomma. _arraycomma will always know the last item in your array and will not put a comma after the last value. Otherwise, a comma will be appended after each value.

New Style https://parsley.zesty.io/generate-json-output/

[

{{each articles as article limit 10 order by rand() }}

   {{article.toJSON()}} {{article._arraycomma}}

{{/each}}

]

Ending Each Iterations and If Logic Statements

A simple change to shorten code for an each statement {{end-each}} can now be written as {{endeach}} or {{/each}}.

If statements can now be terminated with {{/if}}, {{endif}} or {{end-if}}

Data Access Calls

Data access calls are streamlined and gain two new functions.

Live examples https://parsley.zesty.io/directly-access-an-item-of-a-content-model-by-zuid/

Previous way to access a single item with parsley content syntax:

Old Style 

{{articles.filter(z.zuid = '{this.articlezuid}').title}}

New Style

{{articles.filter({this.articlezuid}).title}}

Using filter to search

{{articles.filter(title like '%parsley%').title}}

Example of a custom JSON endpoint to get a single item outputted to a fully hydrated JSON object. Make a custom file in code, /data/article.json with code

{{articles.filter({request.queryParam(zuid)}).toJSON()}}

Then call the endpoint with a ZUID www.domain.com/data/article.json?zuid=7-x1y2z3-x1y2z3

 

Have Parsley Ideas?

We are open to hearing your ideas and may incorporate them in the next release. Contact your customer service manager to provide feedback.

By Randy Apuzzo

Randy has had a penchant for computer programming from an early age and started applying his skills to build business software in 2004. Randy's stack of skills range from programming, system architecture, business know-how, to typographic design; which lends to a truly customer-centric and business effective software design. He leads the Zesty.io team as CEO.

Related Articles

Subscribe to the zestiest newsletter in the industry

Get the latest from the Zesty team, from whitepapers to product updates.