简体   繁体   English

尝试在模板中呈现html内容时遇到问题

[英]having problems trying to render html content inside template

Having the following lodash template source 具有以下lodash模板源

<script id="resume-template" type="text/html">
    <tr>
        <td>Curriculum</td>
        <td>
            <%= _.unescape(resume.curriculum) %>
        </td>
    </tr>
</script>

And having the following context 并且具有以下上下文

const context = {
    resume: {
        email: '...',
        firstName: '...',
        lastName: '...',
        curriculum: "&amp;lt;p&amp;gt;some important information goes here&amp;lt;/p&amp;gt;\r\n&amp;lt;p&amp;gt;unordered list&amp;lt;/p&amp;gt;\r\n&amp;lt;ul&amp;gt;\r\n&amp;lt;li&amp;gt;item1&amp;lt;/li&amp;gt;\r\n&amp;lt;li&amp;gt;item2&amp;lt;/li&amp;gt;\r\n&amp;lt;li&amp;gt;item3&amp;lt;/li&amp;gt;\r\n&amp;lt;li&amp;gt;item4&amp;lt;/li&amp;gt;\r\n&amp;lt;/ul&amp;gt;\r\n&amp;lt;p&amp;gt;ordered list&amp;lt;/p&amp;gt;\r\n&amp;lt;ol&amp;gt;\r\n&amp;lt;li&amp;gt;pet1&amp;lt;/li&amp;gt;\r\n&amp;lt;li&amp;gt;pet2&amp;lt;/li&amp;gt;\r\n&amp;lt;li&amp;gt;pet3&amp;lt;/li&amp;gt;\r\n&amp;lt;li&amp;gt;pet4&amp;lt;/li&amp;gt;\r\n&amp;lt;/ol&amp;gt;\r\n&amp;lt;p&amp;gt;Some &amp;lt;span style=&amp;quot;text-decoration: line-through;&amp;quot;&amp;gt;&amp;lt;span style=&amp;quot;text-decoration: underline;&amp;quot;&amp;gt;&amp;lt;strong&amp;gt;other&amp;lt;/strong&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;span style=&amp;quot;text-decoration: underline;&amp;quot;&amp;gt;information&amp;lt;/span&amp;gt; &amp;lt;span style=&amp;quot;text-decoration: line-through;&amp;quot;&amp;gt;goes&amp;lt;/span&amp;gt; &amp;lt;strong&amp;gt;here&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;"
    }
}

And the following implementation 以及以下实现

const app = document.querySelector('#app');
const resume = response; // ajax response
const source = document.querySelector('#resume-template').innerHTML;
const template = _.template(source);

app.innerHTML = template(resume);

I need to render the content of the resume.curriculum key as html, in order of accomplish this I first unescape the resume.curriculum content <%= _.unscape(resume.curriculum) %> , but instead of getting the html, I get the html tags and their content as text 我需要将resume.curriculum键的内容呈现为html,为了实现此目的,我首先取消对resume.curriculum content <%= _.unscape(resume.curriculum) %>转义,但是我没有获取html,而是获取html标签及其内容作为文本

As a help, if in template I place the following content <%= '<p><i>Some content</i></p>' %> is appropriately rendered as html 作为帮助,如果在模板中放置以下内容<%= '<p><i>Some content</i></p>' %> ,则应适当地呈现为html

I appreciate any help to solve this scenario 感谢您为解决此问题提供的帮助

在此处输入图片说明

Somewhere in your server-side code you have double-escaped your curriculum field. 在服务器端代码中的某个位置,您已经两次转义了curriculum字段。 See this chunk: 看到这个块:

&amp;lt;p&amp;gt;some important information goes here&amp;lt;/p&amp;gt;

That should be: 应该是:

&lt;p&gt;some important information goes here&lt;/p&gt;

So, fix your server-side code and you'll see your stuff start working. 因此,修复服务器端代码,您将看到您的东西开始工作。 To verify this, just change this: 要验证这一点,只需更改以下内容:

<td>
  <%= _.unescape(resume.curriculum) %>
</td>

to: 至:

<td>
  <%= _.unescape(_.unescape(resume.curriculum)) %>
</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM