【问题标题】:parse ckeditor content and apply tags instead of displaying them on the screen解析ckeditor内容并应用标签而不是在屏幕上显示它们
【发布时间】:2018-02-11 15:25:40
【问题描述】:

我在解析 CKEditor 的内容时遇到问题。我已将数据保存到 mongoDB 中,但是当我检索要显示的内容时,它会显示标签而不是解析它们。

内容在我保存时采用了这种格式。这就是当我从数据库中检索它时,我希望它显示的方式。

但这是我从数据库中检索时得到的内容。我希望它应用标签但不显示它们。所以我可以使用与输入相同的格式。

<p>The <strong>content had thi</strong>s formatting <em>when I saved it. This is how</em> I want it to display <strong><em>when I retrieve it from the database</em></strong>.</p>

当我尝试显示内容时,我的 html 页面如下:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">


    <script src="https://cdn.ckeditor.com/4.8.0/standard/ckeditor.js"></script>

</head>

<body>    
    <div id="pgContent">
        <div class="container">
            <span class="line"></span>
            <div id="msgContent" style="max-width:800px; font-size:16px;">&lt;p&gt;The &lt;strong&gt;content had thi&lt;/strong&gt;s formatting &lt;em&gt;when I saved it. This is how&lt;/em&gt; I want it to display &lt;strong&gt;&lt;em&gt;when I retrieve it from the database&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;</div></div></div>

</body>

没有添加 jquery/javascript。我添加了 javascript,但它再次将 div 转换为编辑器:

         CKEDITOR.replace('msgContent',
{
    entities: false,
    basicEntities: false,
    entities_greek: false,
    entities_latin: false,
    htmlDecodeOutput:true,
}

我在写消息时使用了以下内容:'

         CKEDITOR.replace('msgEditor')

更新: 数据库(mongoDB)中的数据如下所示:

<p>The <strong>content had thi</strong>s formatting <em>when I saved it. This is how</em> I want it to display <strong><em>when I retrieve it from the database</em></strong>.</p>\r\n

谢谢你

【问题讨论】:

  • 检查数据库中的数据是否也是html编码的。此外,没有 htmlDecodeOutput 配置选项。
  • HI 向导,我删除了 htmlDecodeOutput 选项,但仍然是同样的问题。您对如何正确显示和格式化文本有任何(其他)想法吗?谢谢
  • 既然您已经检查了数据库中的数据是好的,那么您使用什么命令来输出数据?您是否使用 PHP 和 htmlspecialcharshtmlentities
  • 当我获取数据时,我只是将它发送到视图引擎(车把)。我没有做任何特别的事情。我正在使用 NodeJS、Handlebars 和 MongoDB。下面是渲染数据的promise。
  • 你能把Handlebars模板中影响msgContent的部分贴出来吗?

标签: html ckeditor


【解决方案1】:

根据Handlebars docs:

Handlebars HTML 转义 {{expression}} 返回的值。如果你 不想让 Handlebars 转义一个值,请使用“triple-stash”,{{{.

因此,在您的模板中将 {{message.content}} 更改为 {{{message.content}}}

【讨论】:

  • 成功了。极好的。你拯救了我的旅程。非常感谢。
  • 太棒了!如果您愿意,请将其标记为正确答案。
  • 我做到了。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
相关资源
最近更新 更多