首页 > 解决方案 > 如何防止 Twirl 从 HTMLentities 对脚本中的字符串进行编码?

问题描述

我正在尝试使用 Twirl 将 JsonLd 嵌入到我的页面中,但由于某种原因,内容被 HTMLentities 编码。

我的(旋转模板)html 如下所示:

@(jsonLd: String)(implicit config: play.api.Configuration)
@main() {
  <script type="application/ld+json">
    @jsonLd
  </script>
}

但是当我查看网络浏览器时,我会看到类似:{&quot;@type&quot;:&quot;DataRecord&quot;,&quot;@context&quot;...等。

如何防止@jsonLd被编码?

标签: playframeworktwirl

解决方案


默认情况下,出于安全原因对所有动态内容进行编码以避免一些常见攻击 (XSS)。见这里:https ://www.playframework.com/documentation/2.8.x/ScalaTemplates#Escaping

您可以使用

@Html(jsonLd)

推荐阅读