简体   繁体   English

Jade无法访问变量,除非将其呈现为{{variable}}

[英]Jade can't access variable unless it's rendered as {{ variable }}

I'm currently rendering a Jade template after passing in values from Backbone.js using _.template. 在使用_.template从Backbone.js传递值之后,我目前正在渲染Jade模板。 I can access anything rendered directly as: 我可以访问任何直接呈现为:

// Jade
{{ variable }}

but I cannot access it using: 但我无法使用以下方式访问它:

// Jade
each item in variable
// exception: variable is not defined

So for instance in the following code I'm trying to send in a resources object that I can iterate over using each, but I can't seem to access it at all UNLESS it's marked as {{ resources }}, but then I can't seem to iterate over it with each in Jade. 因此,例如,在下面的代码中,我试图发送一个资源对象,我可以使用每个对象进行迭代,但是除非标记为{{resources}},否则我似乎根本无法访问它,但是我可以似乎在Jade中没有对每个对象进行迭代。

Backbone.js: Backbone.js:

render: function() {
  this.$el.append( this.todoTpl({
      // data
      resources: this.collection.data.attributes.resources,
    })
  );

  return this;

}

Jade: 玉:

if(typeof resources !== "undefined")
      each key, value in resources
        div.dev
          p= value
          p= key

Which never displays anything. 从不显示任何内容。 I can render [Object, Object] to the HTML if I use {{ resources }} though. 如果使用{{resources}},我可以将[Object,Object]渲染为HTML。

I've tried using locals.resources, among other things, but I can't seem to nail this down and it seems like it should be something dumb and simple. 除了其他方面,我尝试使用locals.resources,但是我似乎无法将其固定下来,似乎应该是愚蠢而简单的事情。

TL;DR: how can I set a variable so that it's accessible to Jade methods and not just rendered as text by {{ variable }}? TL; DR:如何设置一个变量,以便Jade方法可以访问它,而不仅仅是{{variable}}呈现为文本?

Thank you! 谢谢!

UPDATE: Posting additional code for my collection: 更新:为我的收藏集发布其他代码:

var ProjectDataCollection = Backbone.Collection.extend({

  defaults: {
    versionOne: "",
    eCM: "",
    bugzilla: "",
    teamCity: "",
    appData: "",
    data: "",
  },

  model: ProjectModel,

  url: '/data',

  initialize: function(models, options) {
      var id = options.id || [];
      if (id > 0) { 
          this.fetchData(id);
      }
      console.log("A new collection has been created.");
  },

  fetchData: function(id) {
    // Save a reference to the existing url
    var baseUrl = this.url;

    // Assign our batch url to the 'url' property and call the server
    this.url += '/' + id;
    this.fetch({
        success: function(collection, response) {
            collection.appData = new AppData(response.details);
            collection.bugzilla = new Bugzilla(response.apis.bugzilla);
            collection.versionone = new VersionOne(response.apis.versionone);
            collection.trigger('change');
        },
        error: function() {
            console.log("fail");
        }
    });

    // Restore the 'url' property
    this.url = baseUrl;
}

}); });

I avoiding posting all this mostly to try and keep my question simple. 我主要避免发布所有这些内容,以使我的问题保持简单。 I apologize if I left too much out! 如果遗漏太多,我深表歉意! I also KNOW I'm doing some horrible things here due to my inexperience with Backbone :) 我也知道由于对Backbone的经验不足,我在这里做一些可怕的事情:)

UPDATE WITH RESOLUTION: 解决方案更新:

To answer several questions: I'm using Jade and Underscore because Jade templates for HTML are beautiful and concise. 要回答几个问题:我正在使用Jade和Underscore,因为HTML的Jade模板简洁美观。 Backbone is using Underscore to pass in data into JS templates that I'm rendering with Jade. Backbone正在使用Underscore将数据传递到我使用Jade渲染的JS模板中。 Although I haven't seen a lot of people doing this online there have been a few posts where others have preferred this arrangement. 尽管我没有看到很多人在网上进行此操作,但仍有一些帖子其他人更喜欢这种安排。

user1737909's comments helped me to find my solution. user1737909的评论帮助我找到了解决方案。 When using _.template I was passing in an override to the templateSettings global to utilize {{ }} for my interpolation (a holdover from another template engine I've used). 使用_.template时,我将覆盖传递给templateSettings全局变量,以利用{{}}进行插值(来自我使用的另一个模板引擎的保留)。 Underscore will print out straight values in this way but it won't process script such as: 下划线将以这种方式打印出直接值,但不会处理以下脚本:

{{ _.each(list, function() {} }}

Once I removed my override settings: 删除替代设置后:

<% _.each(list, function() {} %>

was quite happy and worked perfectly. 很高兴,工作得很好。 Although it seems strange to use two template engines I have to say Jade is so nice it was still worth the pain to enjoy its syntax :) 虽然使用两个模板引擎似乎很奇怪,但我不得不说Jade非常好,但是仍然值得享受它的语法苦恼:)

Thanks for the feedback! 感谢您的反馈!

Jade插值是#{var} ,而不是{{ var }} ,所以我想您不是将变量传递给Jade而是传递给其他东西(例如把手,甚至是角度的东西)

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

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