[英]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: "&lt;p&gt;some important information goes here&lt;/p&gt;\r\n&lt;p&gt;unordered list&lt;/p&gt;\r\n&lt;ul&gt;\r\n&lt;li&gt;item1&lt;/li&gt;\r\n&lt;li&gt;item2&lt;/li&gt;\r\n&lt;li&gt;item3&lt;/li&gt;\r\n&lt;li&gt;item4&lt;/li&gt;\r\n&lt;/ul&gt;\r\n&lt;p&gt;ordered list&lt;/p&gt;\r\n&lt;ol&gt;\r\n&lt;li&gt;pet1&lt;/li&gt;\r\n&lt;li&gt;pet2&lt;/li&gt;\r\n&lt;li&gt;pet3&lt;/li&gt;\r\n&lt;li&gt;pet4&lt;/li&gt;\r\n&lt;/ol&gt;\r\n&lt;p&gt;Some &lt;span style=&quot;text-decoration: line-through;&quot;&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;strong&gt;other&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;text-decoration: underline;&quot;&gt;information&lt;/span&gt; &lt;span style=&quot;text-decoration: line-through;&quot;&gt;goes&lt;/span&gt; &lt;strong&gt;here&lt;/strong&gt;&lt;/p&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: 看到这个块:
&lt;p&gt;some important information goes here&lt;/p&gt;
That should be: 应该是:
<p>some important information goes here</p>
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.