最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass object literal to handlebars partial - Stack Overflow

programmeradmin6浏览0评论

Is it possible to do something like the following in a handlebars template?

{{> myPartial {name: 'steve', age: '40'} }}

This throws a parse error on {. I'm fine with passing either a context or named parameter.

docs:

It's possible to execute partials on a custom context by passing in the context to the partial call. {{> myPartial myOtherContext }}

I'm using webpack with handlebars-loader to render my templates and I don't really have anywhere to pass in a context. I simply want to use this partial in multiple templates and specify the data at that time.

Is it possible to do something like the following in a handlebars template?

{{> myPartial {name: 'steve', age: '40'} }}

This throws a parse error on {. I'm fine with passing either a context or named parameter.

docs:

It's possible to execute partials on a custom context by passing in the context to the partial call. {{> myPartial myOtherContext }}

I'm using webpack with handlebars-loader to render my templates and I don't really have anywhere to pass in a context. I simply want to use this partial in multiple templates and specify the data at that time.

Share Improve this question edited Apr 1, 2016 at 18:08 aw04 asked Apr 1, 2016 at 15:55 aw04aw04 11.2k11 gold badges59 silver badges91 bronze badges 1
  • A added a recent update in case – Arthur Commented Aug 2, 2017 at 18:01
Add a comment  | 

2 Answers 2

Reset to default 14

Related to this reply : https://github.com/wycats/handlebars.js/issues/476 You cannot assign new variable on Handlebars template, you must create this object {name: 'steve', age: '40'} on template data.

data.user = {name: 'steve', age: '40'}

on .hbs

{{> myPartial user}}

But you can take a look to private variables : http://handlebarsjs.com/block_helpers.html

---- UPDATE August 2017 ----

With the new let block helper instruction you can create HTML variable to manipulate easier your display logic:

Global JS

// Create a new Global helper, available everywhere
Template.registerHelper('getUser', function () {
  return Meteor.user()
})

Blaze HTML

Use your global helper to retrieve the user into a variable and use it into HTML

{{#let user=getUser}}
  {{#if user}}
    Hi {{user.name}}
  {{else}}
    Please Login
  {{/if}}
{{/let}}

---- UPDATE 2019 ----

Let reopen that question and put it to the next level.

By registering new simple helpers you will be able to create object or array directly from Blaze (so everything you want):

Template.registerHelper('object', function({hash}) {
  return hash;
})
Template.registerHelper('array', function() {
  return Array.from(arguments).slice(0, arguments.length-1)
})

Usage:

{{> myPartial (object name="steve" age=40 array_example=(array true 2 "3"))}}

Will send as context:

 {
   name: 'steve', 
   age: 40,
   array_example: [true, 2, "3"] 
 }

register a helper (i.e. context) for parsing the context you want to pass:

here is how: https://jsfiddle.net/dregep/o4p1g8nL/11/

Handlebars.registerHelper('context', function (jsString) {
    return eval("context=" + jsString);
});

then call it like this:

{{> myPartial (context 'js object') }}

example:

{{> myPartial (context '{a: 1}') }}

this is equivalent to:

{{> myPartial a=1 }}
发布评论

评论列表(0)

  1. 暂无评论