简体   繁体   English

包含JavaScript撇号的Escape Mustache变量

[英]Escape Mustache variable containing apostrophe for JavaScript

I have a variable ( {{title}} ) which contains an apostrophe. 我有一个变量( {{title}} ,其中包含撇号。 Mustache escapes this as ' 小胡子将其转义为' .

However, the following template results in a JavaScript error ( Expected token ')' ): 但是,以下模板导致JavaScript错误(“ Expected token ')' ):

<a href="javascript:confirm('{{title}}?');">{{title}}</a>

After Mustache rendering, the syntax error is clear ( 'Joe's Lame?' ): 渲染小胡子后,语法错误已清除( 'Joe's Lame?' ):

<a href="javascript:confirm('Joe's Lame?');">Joe's Lame</a>

I am still learning Mustache and while this example is contrived, what is the proper way to escape variables in these situations. 我仍在学习Mustache,并且在构想此示例的同时,在这些情况下转义变量的正确方法是什么。

The fiddle for reference. 小提琴供参考。

So if it doesn't have to be Mustache, you can use a superset of Mustache called Handlebars . 因此,如果不必一定是Mustache,则可以使用称为Handlebars的Mustache超集。

First register a Handlebars helper: 首先注册一个Handlebars助手:

Handlebars.registerHelper('escapeJs', function(str) {
    return str.replace(/[\'\"\\\/]/gm, function (c) {
        return '\\' + c;
    });
});

And you call your helper like this {{escapeJs title}} : 您这样称呼您的帮手{{escapeJs title}}

var view = {
    title: "Joe's Lame\"\\/€"
};

var template = Handlebars.compile(
    "<a href=\"javascript:confirm('{{escapeJs title}}');\">{{title}}</a>");
var output = template(view);

View it live in this fiddle . 观看它生活在这个小提琴中

Mustache is really cool and it's available in almost any programming language. 胡子真的很酷,几乎所有编程语言都可以使用。 Handlebars is awesome and is used eg in Backbone Thorax and assemble , a powerful static web-site generator. 车把很棒,并且被用于例如Backbone Thoraxassemble (功能强大的静态网站生成器)中。

Edit: Alternative Solution 编辑:替代解决方案

When using ECMAScript 5 (and with shim/shiv/polyfills that should be working with IE8 as well), one could prepare the view-object for Mustache in the following way. 使用ECMAScript 5(以及应与IE8一起使用的shim / shiv / polyfills)时,可以按以下方式为Mustache准备视图对象。 I admit, that this is not a very handy solution, but when producing JavaScript output is rare, it might be acceptable IMO. 我承认,这不是一个非常方便的解决方案,但是当很少生成JavaScript输出时,IMO可能会接受。

function escapeJs (str) {
    return str.replace(/[\'\"\\\/]/gm, function (c) {
        return '\\' + c;
    });
}

var view = {
    title: "Joe's Lame"
};

Object.defineProperty(view, 'titleJsEnc', {
    enumerable: false,
    get: function () { return escapeJs(this.title); }
});

var output = Mustache.render(
    "<a href=\"javascript:confirm('{{titleJsEnc}}');\">{{title}}</a>", view);

Defining a new property (via Object.defineProperty ) does the trick. 定义一个新属性(通过Object.defineProperty )就可以了。 One could write a generic object decorator which defines a getter for each real property returning the escaped string value. 可以编写一个通用的对象装饰器,为每个返回转义的字符串值的实际属性定义一个吸气剂。

Here is the fiddle 这是小提琴

Edit: Alternative: Wait for a new version of Mustache 编辑:替代:等待新版本的胡子

Once this pull-request is merged and a new version of Mustache is published, this problem should be solved in a "Mustache native way". 合并此请求请求并发布新版本的Mustache后,应以“ Mustache本机方式”解决此问题。

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

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