var dataArr = [{
name: 'name',
value: 'lcat'
}, {
name: 'score'
value: '123.852',
filter: 'round'
},{
name: 'groups'
value: [1,2,3,6,0],
filter: 'sort'
}]
{% for data in dataArr %}
<div>{{ data[value] | data.filter }}<div>
{% endfor %}
The console error: Error: filter not found: data.filter.
How do I write the loop?
(1) You can use
ifto branch the loop based on a condition observed in the dataset. For example thisNunjuckscode:produces this
htmlcode:From your dataset.
You can play with it in this jsFiddle: http://jsfiddle.net/xmojmr/gbLLryuz/
(2) Or you can roll your own filter which will take the arbitrary filter expression string defined in the dataset and inject the code (unprotected) into the page building engine (You can read little bit about why intentionally allowing code injection is not good idea at Wikipedia: Code injection)
For example if
envvariable is of type Nunjucks.Environment then adding following filter before you run theJavaScripttemplate rendering codeenables the use of simplified equivalent
NunjuckscodeThe above code produces following
htmlcode:when applied to the dataset below (notice the new round(2)):
You can play with it in this jsFiddle: http://jsfiddle.net/xmojmr/jkb7ry9x/1/
The way (1) is safe and reasonably fast but it assumes that you know the list of allowed custom filters ahead
The way (2) is wild, unsafe and slower but enables using of any user-provided filter expression