I have a private template in which a JSON string can be passed in but when the string is not JSON compatible I want to provide something helpful, not just have the template fail.
For example, I'm dynamically building an entries query which could have field scopes or other entry query parameters appended, so I want an optional filters
URL parameter to be allowed to pass a JSON string.
- Thus a parameter of
filters={"level":3}
passes valid JSON butfilters={level:3}
does not. - Let's say the parameter is captured in a variable called
filters
… - So in the first case
filters|json_decode
works but in the second it generates an error.
I'd like to be able to do something like:
{% set valid = filters|json_decode is iterable %}
…and then provide error messaging on false
.
But I can't. I just get a yii syntax error template failure when the string isn't valid.
Is there a solution?