Render a variable as HTML in EJS

…衆ロ難τιáo~ 提交于 2019-11-26 11:49:43

问题


I am using the Forms library for Node.js (Forms), which will render a form for me on the backend as so:

var signup_form = forms.create({
    username: fields.string({required: true})
    , password: fields.password({required: true})
    , confirm:  fields.password({
        required: true
        , validators: [validators.matchField(\'password\')]
    })
    , email: fields.email()
});
var signup_form_as_html = signup_form.toHTML();

The final line var signup_var signup_form_as_html = signup_form.toHTML(); creates a block of HTML which looks as such:

<div class=\"field required\"><label for=\"id_username\">Username</label><input type=\"text\" name=\"username\" id=\"id_username\" /></div><div class=\"field required\"><label for=\"id_password\">Password</label><input type=\"password\" name=\"password\" id=\"id_password\" /></div><div class=\"field required\"><label for=\"id_confirm\">Confirm</label><input type=\"password\" name=\"confirm\" id=\"id_confirm\" /></div><div class=\"field\"><label for=\"id_email\">Email</label><input type=\"text\" name=\"email\" id=\"id_email\" /></div>

Basically just a long string of HTML. I then try to render it using EJS and Express using the following code:

res.render(\'signup.ejs\', {
    session: loginStatus(req)
    , form: signup_form_as_html
});

But on rendering the HTML is simply the string that I posted above, rather than actual HTML (and thus a form as I want). Is there any way to make that string render as actual HTML using EJS? Or will I have to use something like Jade?


回答1:


With ejs you can have

<% code %> 

... which is code that is evaluated but not printed out.

<%= code %>

... which is code that is evaluated and printed out (escaped).

<%- code %>

... which is code that is evaluated and printed out (not escaped).

Since you want to print your variable and NOT escape it, your code would be the last type (with the -<%). In your case:

<%- my_form_content %>

For more, see the full ejs documentation




回答2:


October 2017 update

The new ejs (v2, v2.5.7) development is happening here: https://github.com/mde/ejs The old ejs (v0.5.x, 0.8.5, v1.0.0) is available here https://github.com/tj/ejs

Now with ejs you can do even more. You can use:

  • Escaped output with <%= %> (escape function configurable)
  • Unescaped raw output with <%- %>
  • Newline-trim mode ('newline slurping') with -%> ending tag
  • Whitespace-trim mode (slurp all whitespace) for control flow with <%_ _%>
  • Control flow with <% %>

So, in your case it is going to be <%- variable %> where variable is something like

var variable = "text here <br> and some more text here";

I hope this helps someone. 🙂




回答3:


I had the same issue with rendering the textarea input from from a wysiwyg editor saved as html in my database. The browser will not render it but displayed the html as text. After hours of searching, I found out

<%= data %> escaped data while

<%- data %>left data 'raw'(unescaped) and the browser could now render it.



来源:https://stackoverflow.com/questions/10326950/render-a-variable-as-html-in-ejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!