Print raw html strings on EJS

前端 未结 3 1160
悲哀的现实
悲哀的现实 2020-11-28 11:57

I\'m using express.js with EJS templates and i\'m trying to do something like this:

<%= \"Test\" %>

but

相关标签:
3条回答
  • 2020-11-28 12:18

    for raw output html in ejs you can use this code

    <%- "<a href='#'>Test</a>" %>
    
    0 讨论(0)
  • 2020-11-28 12:32

    You should use html code everywhere, and use the EJS tags only where you need dynamic data. Example:

    <a href='<%= user.id %>'><%= user.name %</a>
    

    To specifically answer your question you can use <%- "<tags_here>" %> to output unescaped HTML data.

    0 讨论(0)
  • 2020-11-28 12:35

    This are the available options according to the docs

    1. <% 'Scriptlet' tag, for control-flow, no output
    2. <%_ ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it
    3. <%= Outputs the value into the template (HTML escaped)
    4. <%- Outputs the unescaped value into the template
    5. <%# Comment tag, no execution, no output
    6. <%% Outputs a literal '<%'
    7. %> Plain ending tag
    8. -%> Trim-mode ('newline slurp') tag, trims following newline
    9. _%> ‘Whitespace Slurping’ ending tag, removes all whitespace after it

    Looks like the option you need is the number 4

    0 讨论(0)
提交回复
热议问题