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

Add json filter for debugging objects? #192

Closed
pdehaan opened this issue Feb 18, 2020 · 4 comments
Closed

Add json filter for debugging objects? #192

pdehaan opened this issue Feb 18, 2020 · 4 comments

Comments

@pdehaan
Copy link
Contributor

pdehaan commented Feb 18, 2020

In Nunjucks there is a dump(n) filter which is basically JSON.stringify(obj, null, n) (if I understand correctly).

I'm struggling to find something similar w/ Liquid.
Closest I've found is Shopify/liquid#121 (and a couple other references to a json) filter, but I cannot seem to get that to work w/ liquidjs.

const { Liquid } = require('liquidjs');
const engine = new Liquid();

engine
    .parseAndRender('{{ name | json }}', {name: {first:'alice', last: 'poppins'}})
    .then(console.log);     // outputs '[object Object]'

const pkg = require("liquidjs/package.json");
console.log(`${pkg.name} @ v${pkg.version}`); // liquidjs @ v9.7.0

UPDATE: Ah, OK, so if I enable the Boolean strictFilters option, I get a hard error saying:

ParseError: undefined filter: json, line:1, col:1

@pdehaan
Copy link
Contributor Author

pdehaan commented Feb 18, 2020

Workaround:

engine.registerFilter('json', (v, n=0) => JSON.stringify(v, null, n));

@harttle
Copy link
Owner

harttle commented Feb 20, 2020

maybe better to use util.inspect(), I'll see how it's done in shopify/liquid

@harttle harttle closed this as completed in aa27a6c Mar 3, 2020
harttle pushed a commit that referenced this issue Mar 3, 2020
# [9.10.0](v9.9.0...v9.10.0) (2020-03-03)

### Features

* support json filter, closes [#192](#192) ([aa27a6c](aa27a6c))
* with & for in `render` tag, closes [#195](#195) ([6ea6881](6ea6881))
@harttle
Copy link
Owner

harttle commented Mar 3, 2020

🎉 This issue has been resolved in version 9.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@harttle
Copy link
Owner

harttle commented Mar 3, 2020

A json filter is builtin from 9.10.0 on, see:

'json': function (v: any) {
return JSON.stringify(v)
}

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

No branches or pull requests

2 participants