Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunjucks nl2br does not exist?

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?

like image 731
Renatho De Carli Rosa Avatar asked Feb 05 '16 16:02

Renatho De Carli Rosa


People also ask

How do you define a variable in Nunjucks?

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 ).

What is Nunjucks used for?

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.

How do I comment in Njk?

Comments. You can write comments using {# and #} . Comments are completely stripped out when rendering.


1 Answers

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.

like image 99
somebody Avatar answered Sep 21 '22 12:09

somebody