Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing / Echoing / Dumping Arrays #121

Closed
reggi opened this issue May 9, 2012 · 12 comments
Closed

Printing / Echoing / Dumping Arrays #121

reggi opened this issue May 9, 2012 · 12 comments

Comments

@reggi
Copy link

reggi commented May 9, 2012

I'm looking for a way in liquid to see what something has before looping it. I'm working with shopify and I hate to have to consult the docs to see what is inside the array "product" I would love to be able to just {{product}} and see some kind of messy array with key value pairs (json would be nice) that I can visually parse and understand. I work a lot with php and javascript and use var_dump() and console.log() excessively and it bothers me that in liquid there is no way to see what is going on within an array. Why hasn't this been built into liquid? or if it has, could someone please share?

@reggi
Copy link
Author

reggi commented Jun 5, 2012

Stumbled upon the filter {{ variable | json }} and it works like a charm.

@reggi reggi closed this as completed Jun 5, 2012
@chrisbo
Copy link

chrisbo commented Jan 9, 2016

Good one :)

@aniruddhasm
Copy link

Thanks it worked 👍

@juanpasolano
Copy link

It doesn't work with everything...
Sometimes it throws {"error":"json not allowed for this object"}
Being new to shopify it'd be nice to be able to dump almost anything. or the whole "context" of a section for example

@tdhatcher
Copy link

tdhatcher commented Dec 13, 2016

Here's how I do it

Create a snippet named "dump.liquid" with the following code snippet
Note: if you use a different filename for the liquid snippet, make sure the variable being passed through liquid's json filter is also the same name, otherwise this shorthand usage won't work as expected

'dump.liquid'
<script type="text/javascript">
 console.log("Dumping");
 console.log({{ dump | json }});
</script>

Usage in template files:
{% include 'dump' with [OBJECT] %}

Example: Used within product.liquid template file
'product.liquid' {% include 'dump' with product %}

Open Chrome console and you will see an object that you can drill into and explore.

@horatiua
Copy link

horatiua commented Nov 9, 2017

For {{ linklists | json }} it throws {"error":"json not allowed for this object"}.
Any fix?

@cuichengshan
Copy link

Very useful but how do you format the output?

@str
Copy link

str commented Apr 4, 2018

I'm still looking for a way to get pass the "not allowed for this object" issue.

@Enelar
Copy link

Enelar commented Aug 18, 2018

still no response for debug dumping variant metafields {"error":"json not allowed for this object"}

@airplanenoise
Copy link

{{ variableName | pprint }}

@VizualAbstract
Copy link

This is Shopify liquid. pp not available.

@verybluebot
Copy link

thanks @tdhatcher - this is the most elegant solution I found for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests