Using Bladejs with Meteor

2019-05-17 02:07发布

问题:

I recently added the node-blade smart package to my meteor and have static content displaying fine. However, I'm not able to use any template variables. Before I installed blade, the template variables worked fine with handlebars. Does anybody know what I'm doing wrong?

console output

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

回答1:

EDIT: Helpers are now permitted in body templates.

You cannot use helpers or certain global variables in head or body templates. You can't even use them in templates that are included by head or body templates.

Checkout these links for more information:

  • https://github.com/bminer/node-blade/issues/98
  • https://github.com/bminer/node-blade/wiki/Using-Blade-with-Meteor


回答2:

EDIT: This answer is no longer accurate as of Blade 3.0.0 stable. body.blade templates may not contain dynamic stuff like helpers, references to Session, etc.


In 'Using Blade with Meteor' says that

References to Session are not allowed in head or body templates. This is by design, and it is not a bug. In Handlebars, you could use Session or Meteor within a tag, but not a tag. I didn't like the Handlebars implementation, so you're stuck with this one. The body.blade template is mostly for static content (i.e. a loading page or whatever). Once your application is loaded, you can do $("body").replaceWith(Meteor.ui.render(Template.homepage) ); from your application code.

So, this is saying that, on initialization, one could not have dynamic generated templates.

To workaround this, documentation suggests

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

I replaced replaceWith method with html method. See an example that's working for me:

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

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

See the compiled JavaScript:

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

Don't know if there is a shorter way to write it.