I need a filter like the Jinja "nl2br", but in the Nunjucks. In the documentation are a mention (https://mozilla.github.io/nunjucks/templating.html), but I searched it in the nunjucks code (https://github.com/mozilla/nunjucks/blob/master/src/filters.js) and it does not exist.
Somebody knows how to solve it with a equivalent filter or another solution? Or I need to create the filter?
var njglobals = require('nunjucks/src/globals'); njglobals. someVar = 'someValue'; You can now use someVar in your templates. Be sure not to overwrite any of the existing properties of the njglobals object, though (for [email protected] , they are range , cycler and joiner ).
A rich, high-performance JavaScript templating language, supported by all modern browsers. Nunjucks is customizable with extensions and filters; it offers inheritance, asynchronous control, autoescaping and other features. It also supports any version of Node.
Comments. You can write comments using {# and #} . Comments are completely stripped out when rendering.
Nunjucks has built-in escaping. If you set {autoescape: true}
when settings up Nunjucks, then you don't need to do anything. Otherwise, you can use the escape
filter.
If you just want to escape newlines, then do this:
env.addFilter('nl2br', function(str) {
return str.replace(/\r|\n|\r\n/g, '<br />')
})
and use the newly created nl2br
filter.
Note: env
is your Nunjucks environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With