split string to array

1,693 views
Skip to first unread message

cdel...@twitter.com

unread,
Oct 21, 2016, 4:07:42 PM10/21/16
to Nunjucks
Hello!

I was wondering if there was a way to convert a string into an array.

This is possible in Jinja2 by using the "split" filter:

{% set candy = "twix|kit kat|butterfinger|snickers" %}
{% set candyArray = candy|split("|") %}

Does it go by another name in Nunjucks?

Thanks!
Chris
Message has been deleted

Vikas Sharma

unread,
Jun 5, 2018, 1:09:16 PM6/5/18
to Nunjucks
Dear Chris

Please find Nunjucks filter solution without making custom filter, another option is to  make custom filter and return value.

var nunjucks = require('nunjucks');
var env = new nunjucks.Environment(null, { autoescape: false });

var pipe = '{% set candy = "twix|kit kat|butterfinger|snickers" %}{% set candyArray = candy.split("|") | dump %}{{candyArray}}';

var source = nunjucks.compile(pipe, env);
var template = source.render(pipe);

// Need to parse template value as we implemented dump filter
template = JSON.parse(template);
    
console.log(' -- template: ' + template[1]);
Reply all
Reply to author
Forward
0 new messages