如何格式化中的骨干机型回车在胡子模板 [英] How to format carriage returns in a Backbone model in a Mustache template

查看:195
本文介绍了如何格式化中的骨干机型回车在胡子模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的骨干机型作为输入,胡子模板来生成HTML。

I'm using Backbone models as input into Mustache templates to generate HTML.

我有许多属性,诸如名称,描述和id的骨干模型。说明属性可以包含回车,我想呈现为< BR> 标签时,他们在模板正在呈现

I have a Backbone model with a number of attributes, such as name, description and id. The description attribute can contain carriage returns, which I want to render as <br> tags when they're rendered in the template.

在默认情况下,小胡子只是输出马车直接返回,所以标识会看上去整齐,但呈现的结果没有休息。

By default, Mustache simply outputs the carriage returns directly, so the markup looks tidy, but the rendered result has no breaks.

我特别不希望替换\\ n \\ r在描述属性,因为该属性可以被用于其它地方(例如,在ALT或meta标签)。

I don't particularly want to replace \n\r in the description attribute, as that property could be used elsewhere (e.g. in alt or meta tags).

我至今唯一的想法是添加了格式化文本重复描述属性。

The only idea I have so far is to add a duplicate description attribute that has the formatted text.

有没有在这胡子HTML格式换行的&LT; BR&GT; 标签

Is there nothing in Mustache that formats HTML line breaks as <br> tags?

推荐答案

胡子非常有目的的限制。如果你需要在一个胡子模板什么特别的东西,你prepare在JavaScript中的数据,使胡子的插值和循环可以处理它。在你的情况,这意味着你的分割上EOLS字符串来获得一个数组:

Mustache is very limited on purpose. If you need anything special in a Mustache template, you prepare your data in JavaScript so that Mustache's interpolation and loops can handle it. In your case, that means splitting your string on EOLs to get an array:

// Adjust the regex to suit your data, this one is pretty loose.
var lines = string.split(/[\r\n]+/)
                  .map(function(line) { return { line: line } });

,然后循环遍历胡子数组:

and then loop over that array in Mustache:

{{#lines}}
    {{line}}<br>
{{/lines}}

这篇关于如何格式化中的骨干机型回车在胡子模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆