bigcommerce / cornerstone

The BigCommerce Cornerstone theme
https://developer.bigcommerce.com/stencil-docs
287 stars 605 forks source link

JSONparse Handlebar Helper #1485

Closed originsid closed 5 years ago

originsid commented 5 years ago

I apologize if this should be posted elsewhere and hope someone can point me in the right direction if that's the case.

While customizing Cornerstone we used the JSONparse handlebar helper, with little documentation on it and no examples to work with. With some trial/error we found that it will parse a JSON string and use the result as the context.

Example of how we're using it when looping through custom fields on a product page: {{#startsWith '{' value}} {{#JSONparse value}} {{#if img}} {{> components/page/hero-banner backgroundImage=(cdn (concat 'livecdn:/product_images/uploaded_images/' img)) }} {{else}} {{> components/page/hero-banner}} {{/if}} {{/JSONparse}} {{else}} {{> components/page/hero-banner backgroundImage=(cdn (concat 'livecdn:/product_images/uploaded_images/' value)) }} {{/startsWith}}

In short, we check to see if the custom field value starts with {, if so then we assume it is valid JSON and parse it for use with the hero-banner component.

We found that if the JSON string we attempt to parse is not valid then this will completely break the page with a server error. Definitely not an ideal solution for what we're doing.

What is the proper usage for JSONparse? Did we discover something that is unintended? Is there a way we can attempt to parse the JSON and fall back to default content if it's invalid (without generating a server error)?

Thank you in advance!

carsonreinke commented 5 years ago

@originsid You want to put this issue under https://github.com/bigcommerce/paper-handlebars and somewhat related to https://github.com/bigcommerce/paper-handlebars/pull/31

originsid commented 5 years ago

@carsonreinke Thank you Carson, posting it there!