简体   繁体   English

将JSON中的HTML作为数据传递到Assemble中

[英]Passing HTML in JSON as data in Assemble

I am working on a project that requires multilanguage support. 我正在开发一个需要多语言支持的项目。 I decided to utilize Assemble (more specifically grunt-assemble) since it was already part of the project toolbox so my current setup uses JSON file(s) as data/text input for handlebar templates. 我决定使用Assemble(更具体地说是grunt-assemble),因为它已经是项目工具箱的一部分,所以我当前的设置使用JSON文件作为车把模板的数据/文本输入。

The site is responsive and there is a requirement to have certain level of control over text using break lines <br /> or non-breaking spaces &nbsp; 该网站响应并没有采用破线比文字控制的一定水平的要求<br />或不间断空格&nbsp; to avoid orphaned words. 避免孤儿话。 Some sentences require mentioned tag or html entity to be included in the string otherwise I'd be forced to split sentence word by word and combine hardcoded html with json data reference. 有些句子要求提及标签或html实体包含在字符串中,否则我将被迫逐字拆分并将硬编码的html与json数据引用相结合。 Imagine something like this: 想象一下这样的事情:

<p>{{word_1}}<br />{{word_2}}</p>

This approach is also not very translation friendly, since a different language might not require line break at all. 这种方法也不是很友好,因为不同的语言可能根本不需要换行。

To avoid this I've tried to pass html via JSON like this: 为了避免这种情况,我试图通过JSON传递html,如下所示:

{ "sentence" : "word<br />word" }

Assemble output, however, is literal, so instead or of functional tag I get its string version and page literally displays word<br />word . 然而,汇编输出是文字的,所以相反或功能标签我得到它的字符串版本和页面字面上显示word<br />word Same for &nbsp; 同样适用于&nbsp;

What is (if any) proper notation for passing html tags or entities from JSON to handlebar templates via Assemble? 什么是(如果有的话)通过Assemble将html标签或实体从JSON传递到车把模板的正确表示法?

Handlebars escapes HTML by default, but you can avoid escaping with the triple-stash format {{{ }}} . Handlebars默认会转义HTML,但您可以避免使用三重隐藏格式{{{ }}}转义。 Take a look at the following .hbs page: 看看下面的.hbs页面:

---
title: Test
htmlData: This is some <br/> html in data
---
<p>double-stash: {{htmlData}}</p>
<p>triple-stash: {{{htmlData}}}</p>

results in: 结果是:

double-stash: This is some <br/> html in data

triple-stash: This is some
html in data

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

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