【问题标题】:Using Bladejs with Meteor将 Bladejs 与 Meteor 一起使用
【发布时间】:2012-10-23 19:23:04
【问题描述】:

我最近将 node-blade 智能包添加到我的流星中,并且静态内容显示正常。但是,我无法使用任何模板变量。在我安装刀片之前,模板变量在车把上工作得很好。有人知道我做错了什么吗?

控制台输出

ReferenceError: player is not defined
    at ~/meteor/project/views/info.blade:1:1

1 > .player-name.large= player.name
2 | .alliance-name= alliance.name
3 | .world-name= world.name
4 | 

    at eval (eval at <anonymous> (/usr/local/lib/node_modules/blade/lib/compiler.js:138:23))
    at /usr/local/lib/node_modules/blade/lib/runtime.js:323:5
    at runtime.loadTemplate (/usr/local/lib/node_modules/blade/lib/runtime.js:272:6)
    at /usr/local/lib/node_modules/blade/lib/blade.js:45:4
    at Compiler.compile (/usr/local/lib/node_modules/blade/lib/compiler.js:185:2)
    at compile (/usr/local/lib/node_modules/blade/lib/blade.js:41:12)
    at Object.compileFile (/usr/local/lib/node_modules/blade/lib/blade.js:66:3)
    at Object.runtime.loadTemplate (/usr/local/lib/node_modules/blade/lib/runtime.js:269:23)
    at Object.runtime.include (/usr/local/lib/node_modules/blade/lib/runtime.js:320:22)
    at eval (eval at <anonymous> (/usr/local/lib/node_modules/blade/lib/compiler.js:138:23))
Your application is crashing. Waiting for file change.

info.blade

.player-name.large= player.name

client.js

if(Meteor.is_client) {
    Template.info.player = function(){
        var data = Session.get( 'data' );
        return data.player;
    };
}

【问题讨论】:

  • 您使用的是 body.blade 模板吗?如果是这样,您的 body.blade 文件是否包含您的 info.blade 文件?
  • 它包含一个模板,而该模板又包含此模板
  • 同样的问题。并且只包含一个模板。

标签: meteor blade node-blade


【解决方案1】:

编辑: 自 Blade 3.0.0 稳定版起,此答案不再准确。 body.blade 模板可能不包含动态内容,如帮助程序、对 Session 的引用等。


'Using Blade with Meteor' 中说

头部或正文模板中不允许引用 Session。这是设计使然,它不是错误。在 Handlebars 中,您可以在标签中使用 Session 或 Meteor,但不能在标签中使用。我不喜欢 Handlebars 的实现,所以你被这个卡住了。 body.blade 模板主要用于静态内容(即加载页面或其他)。加载应用程序后,您可以执行$("body").replaceWith(Meteor.ui.render(Template.homepage) );来自您的应用程序代码。

也就是说,在初始化时,不能动态生成模板。

为了解决这个问题,文档建议

$("body").replaceWith(Meteor.ui.render(Template.homepage) )

我用html 方法替换了replaceWith 方法。查看一个对我有用的示例:

# ./the_cow.coffee
if Meteor.isClient
  $ ->
    $('body').html Meteor.render -> Template.test
      user:
        name: 'Pill'

# ./views/test.blade
#test Testing
p= user.name

查看编译后的 JavaScript:

if (Meteor.isClient) {
  $(function() {
    return $('body').html(Meteor.render(function() {
      return Template.test({
        user: {
          name: 'Pill'
        }
      });
    }));
  });
}

不知道有没有更短的写法。

【讨论】:

    【解决方案2】:

    编辑:现在允许在正文模板中使用助手。

    您不能在 head 或 body 模板中使用帮助程序或某些全局变量。您甚至不能在 head 或 body 模板包含的模板中使用它们。

    查看这些链接了解更多信息:

    【讨论】:

    • @VanCoding - 现在是,我想。
    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多