问题
I have a pug template that is rendered and shown to the user when a button is clicked on my web page. I have an issue with pug rendering HTML tags as part raw text instead of parsing the text and adding it as a DOM node.
My pug template looks like: p #{object.property}
so trying to interpolate an HTML string into the template.
The property contains an HTML string that looks like:
<p>Here is some text I'm trying to render</p>
I have tried just using #{object.property}
to strange rendering issues where it renders <
in the page followed by the text content. I'm pretty sure this is caused by pug reading the <
as plain text and the p
as a pug element which makes sense.
This is what the render looks like on the rendered web page.
<
Here is some text I'm trying to render
>Here is some text I'm trying to render
>
What I'm trying to accomplish is having reference to a valid HTML string in my pug template and have pug render it as a DOM node.
Having the HTML string without tags is unfortunately out of the question.
Any responses are much appreciated.
Note: Using Pug 2.0.0-beta-11
回答1:
In order to preserve HTML in a string passed to the template, you need to use unescaped string interpolation. Instead of the number sign before brackets, you need an exclamation point. So #{object.property}
should be !{object.property}
来源:https://stackoverflow.com/questions/48959377/pug-template-isnt-rendering-raw-html-passed-to-it