Last active
June 6, 2019 20:17
-
-
Save CharlesRyan/7de60f2bc7a4192986dff5c5454d3f0b to your computer and use it in GitHub Desktop.
A liquid snippet that logs the value of any liquid variable that gets passed to it. It also has the ability to output the name of the variable if needed. There's a couple usage examples at the top of the snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% comment %} | |
data_type options are 'string', 'object', 'iterable strings', 'iterable objects', and null | |
for numbers and booleans, enter a data_type of 'string' | |
it allows for omitting data_type by just logging it as both a string and an object | |
the capture tags can also be omitted if the name of the variable isn't needed | |
{% endcomment %} | |
{% comment %} | |
Usage Examples: | |
{% endcomment %} | |
{% assign myVar = 'some value' %} | |
{% capture varName %}{% raw %}myVar{% endraw %}{% endcapture %} | |
{% include 'logger', data: myVar, varName: varName, data_type: 'string' %} | |
{% comment %} | |
simplest implementation: | |
{% endcomment %} | |
{% include 'logger', data: product.variants %} | |
{% comment %} | |
/logger.liquid | |
{% endcomment %} | |
<script type="text/javascript"> | |
{% if data_type == 'string' %} | |
console.log("Logging {{varName}} as a string"); | |
console.log('{{ data }}'); | |
{% elsif data_type == 'object' %} | |
console.log("Logging {{varName}} as an object"); | |
console.log({{ data | json }}); | |
{% elsif data_type == 'iterable objects' %} | |
console.log("Logging {{varName}} as iterable objects"); | |
{% for thing in data %} | |
console.log({{ thing | json }}); | |
{% endfor %} | |
{% elsif data_type == 'iterable strings' %} | |
console.log("Logging {{varName}} as iterable strings"); | |
{% for thing in data %} | |
console.log('{{ thing }}'); | |
{% endfor %} | |
{% else %} | |
// data_type undefined, log as string and object | |
{% if data >= 1 %} | |
console.log("Logging {{varName}} as a number"); | |
console.log('{{ data }}'); | |
{% elsif data.size > 1 %} | |
console.log("Logging {{varName}} as iterable objects"); | |
{% for thing in data %} | |
console.log({{ thing | json }}); | |
{% endfor %} | |
console.log("Logging {{varName}} as iterable strings"); | |
{% for thing in data %} | |
console.log('{{ thing }}'); | |
{% endfor %} | |
{% else %} | |
console.log("Logging {{varName}} as a string"); | |
console.log('{{ data }}'); | |
console.log("Logging {{varName}} as an object"); | |
console.log({{ data | json }}); | |
{% endif %} | |
{% endif %} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment