繁体   English   中英

如何在underscore.js中使用JST?

[英]How to use JST with underscore.js?

我在使用通常对理解.js没问题的变量时遇到了麻烦,但是似乎当您将JST与underscore.js结合使用时,似乎很麻烦。

var something= SD.defaultView.extend({
    el: 'page',
    template: JST['app/www/js/templates/sex.ejs'],
    data: {
        header: 'some information!!!',
        image: '/img/path.jpg'
    },
    render: function () {
        var compiled = _.template(this.template(), this.data); //I pass in the complied JST template
        this.$el.html(compiled);
    }
});

呈现的JST文件

this["JST"]["app/www/js/templates/sex.ejs"] = function (obj) {
    obj || (obj = {});
    var __t, __p = '', __e = _.escape;
    with (obj) {
        __p += ((__t = ( header )) == null ? '' : __t) + '<sexform>Hello There</sexform>';
    }
    return __p
};

错误

ReferenceError: header is not defined - templates.js (line 21)

...obj = {});var __t, __p = '', __e = _.escape;with (obj) {__p +=((__t = ( header )...

sex.ejs

<%= header %><sexform>Hello There</sexform>

背景资料

正如预期的那样,在阅读器时header不可用,这是通过对我的JST模板进行的每次更改通过grunt文件进行的。 我觉得我必须以错误的方式实施JST。

但是,对我来说,这似乎是正确的做法。

当然,我正在尝试在sex.ejs内部使用带下划线的变量

所有这些代码都可以在这里看到: http : //m.sexdiaries.co.uk/#wank 注意:我可以保证这对工作是安全的,并且不包含任何图像,尽管与url一样具有误导性,但实际上并没有成人资料,这是一个教育性应用。

您可以使用它来定义视图的模板:

template: JST['app/www/js/templates/sex.ejs'],

JST包含函数(或多或少是使用JST样式的预编译模板的全部内容):

this["JST"]["app/www/js/templates/sex.ejs"] = function (obj) {

然后您执行以下操作:

var compiled = _.template(this.template(), this.data);
// function call ----------------------^^

那里有两件事是错误的:

  1. 您已经调用_.template来编译模板。
  2. this.template是已编译的模板函数,预计将被馈入this.data

解决方法非常简单:

var compiled = this.template(this.data);

暂无
暂无

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

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