Nodejs, Jade Escape Markup
element, it gets rendered as actual DOM elements instead of literal chara
Solution 1:
Jade uses the bang to force unescaped output. So you turn regular output to unescaped output with the following construct: !=
If your content is inside an div tag you could do the following:
div!= content
Solution 2:
As an addition, here is another use case which you need to consider:
If you are extrapolating the HTML content using the #{...}
, it will still give the wrong output.
For that use case, you need the !{...}
alternative.
So,
div= varname
becomes
div!= varname
And
div #{varname} is extrapolated badly
becomes
div !{varname} is extrapolated perfectly
Solution 3:
Actually the OP asks for the escaping, not the unescaping. Which I ran into today.
Let assume, that you have varName
variable with <b>FooBar</b>
content.
Then this template will use the escaped value:
#foobar= varName
so it becomes:
<divid="foobar"><b>FooBar</b></div>
If you use the bang operator:
#foobar!= varName
jade won't escape it, so it becomes:
<divid="foobar"><b>FooBar</b></div>
Solution 4:
This is the official way:
code= '<div>This code is' + ' <escaped>!</div>'
<code><div>This code is <escaped>!</div></code>
Solution 5:
It's not built in to Jade, but you can do it with a filter: (This can be added anywhere at the top of app.js.)
require('jade').filters.escape = function( block ) {
return block
.replace( /&/g, '&' )
.replace( /</g, '<' )
.replace( />/g, '>' )
.replace( /"/g, '"' )
.replace( /#/g, '#' )
.replace( /\\/g, '\\\\' )
.replace( /\n/g, '\\n' );
}
Then use the 'escape' filter in your jade file:
h1Code Sample
pre
code
:escape
<div>some text</div>
Output:
<h1>Code Sample</h1><pre><code><div>hi</div></code></pre>
Post a Comment for "Nodejs, Jade Escape Markup"