gpt4 book ai didi

javascript - 如何将 JST 与 underscore.js 一起使用?

转载 作者:行者123 更新时间:2023-11-29 16:14:22 25 4
gpt4 key购买 nike

我在使用 underscore.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 在读者时不可用,这是通过 grunt 文件发生的,每次更改我的 JST 模板。我觉得我必须以错误的方式实现 JST。

但是,对我来说,这似乎是做所有事情的正确方法。

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

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

最佳答案

你有这个来定义 View 的模板:

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);

关于javascript - 如何将 JST 与 underscore.js 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19415210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com