reggi / shopify

Just a place to track issues and feature requests that I have for shopify
3 stars 1 forks source link

json_parse filter #6

Open reggi opened 9 years ago

reggi commented 9 years ago

I originally posted this here https://github.com/Shopify/liquid/issues/432. The discussion should be followed there.

I'd really love a json_parse filter. The below code does not work to convert a json file to a server-side liquid object. It is however, very possible to convert a json file to a javascript variable, but it's sadly not server-side, and bad for SEO to have content rendered by the client. I was going to attempt to break down the string using liquid itself but It's pretty hard. I've created objects with liquid in the past but they're quite crude you can check that out here, the tweet, the gist example.

This is so simple for you guys to implement into production and it's a no brainer.

{% capture quotes %}
  {% include "json.quotes" %}
{% endcapture %}

{% assign quotes = quotes.quotes[0].author | json_parse %}
{{ quotes.quotes[0].author }}

<script>
var quotes = JSON.parse({{ quotes | json }});
document.write(quotes.quotes[0].author);
</script>

I just tried to use the new theming internationalization feature, I couldn't get it to work.

Ideally this type of thing could just be done with locales. But it would still be nice to have a json_parse.

robinbortlik commented 9 years ago

I was able to achieve this functionality by creating parse_json filter and then assign this result to variable in the template. Then you can simple access data as it should be an object. Here is the commit I made to locomotive_cms liquid extension. https://github.com/robinbortlik/liquid_extensions/commit/2b7a67281b55f25045ec4c44813ddd7535b00b3d

{% for event in contents.events %}
   {% assign json = event.content | as_json %}
   {% for ticket in json.tickets %}
       {{ ticket.end_date }}
  {% endfor %}
{% endfor %}
reggi commented 9 years ago

@robinbortlik this issue is specific to Shopify's implementation of liquid on their site. They don't let you extend liquid with ruby. So if I wanted to used a json_parse filter they would have to create it and provide access to it. See here for a list of valid liquid filters http://docs.shopify.com/themes/liquid-documentation/filters