handlebars.js

Rendering templates within helpers in handlebars

≡放荡痞女 提交于 2019-12-03 12:31:07
Hey guys! Because there seem to be no answer on this: Passing variables through handlebars partial yet, I'm currently working on a little workaround to get this work. So, the idea is to register a helper function which renders a specific template with possible values. A bit code makes it better to understand. This is how a I'd invoke my helper: <div> {{myHelper}} </div> This helper is registered with this little code: hbs.registerHelper(name, function (args) { args = args || {}; var template = hbs.compile(fs.readFileSync(__dirname + '/' + file, 'utf8')); return template(args); }); I put this

How to access a calculated field of a backbone model from handlebars template?

跟風遠走 提交于 2019-12-03 11:52:56
问题 I would like to access the calculated fields I have implemented in the model (backbone.js) from the template. Do I need always to define a helper to do it? I think the problem has to do with the way I pass the model to the template. If I pass this.model.toJSON() I have access to the properties but not to the functions I have defined in it. If I pass this.model directly I can access the function but not the properties of the backbone model. 回答1: Always pass this.model.toJSON() to your

How do I make a template conditional based on a collection's size?

落爺英雄遲暮 提交于 2019-12-03 11:40:02
问题 I want to do something like this: <template name="list"> <ul> {{#if items}} {{#each items}} <li>{{itemContents}}</li> {{/each}} {{else}} <li class="placeholder">There are no items in this list.</li> {{/if}} <ul> </template> where items is a Meteor.cursor: Template.list.items = function() { return Items.find(); }; However, the code above doesn't work, as the conditional will evaluate positively even if there are no items (which is mildly surprising because Handlebars evaluates [] as falsey). I

Precompiled Handlebars templates in Backbone with Requirejs?

血红的双手。 提交于 2019-12-03 11:32:47
问题 I've been messing around with a backbone.js app using require.js and a handlebars templates (I've added the AMD module stuff to handlebars) and just read that pre-compiling the templates can speed it up a fair bit. I was wondering how I would go about including the precompiled templates with requirejs. I have a fair few templates to compile (upwards of 15), so i'm not sure if they should all be in the same output file or have their own once compiled. Also, from what it seems, the compiled

NodeJS + Express + Handlebars - failed to locate view “index.html”

孤街浪徒 提交于 2019-12-03 11:29:22
问题 I have been playing a bit with Node.js. I recently started toying with Express and have been setting up a basic app. I wanted to use Handlebars as my view templating engine, but am hitting a wall - failed to locate view "index.html" I have index.html in the same directory as app.js and and so I would think the code below would have no problem locating index.html... I have searched around, but it would seem that comprehensive examples of anything aside from jade are rare... Anyone have

Using variables for a partial template

余生颓废 提交于 2019-12-03 11:23:55
问题 I'm definitely missing something about the way Handlebars works. I need to call different partials depending on the value of a variable. Currently the only way I've found to do it is this: <template name="base"> {{#if a}}{{> a}}{{/if}} {{#if b}}{{> b}}{{/if}} {{#if c}}{{> c}}{{/if}} </template> And in the corresponding JS: Template.base.a = function () { return (mode === "a"); } Template.base.b = function () { return (mode === "b"); } Template.base.c = function () { return (mode === "c"); } .

In an Ember.js Handlebars template, is there a way to have both static and dynamic class attributes?

左心房为你撑大大i 提交于 2019-12-03 11:18:24
Using the already-overused to-do app example, let's say I want an element with a "todo" class (static) and an "is-done" class (dynamic): <div {{bindAttr class="todo isDone"}}> Other stuff in here </div> In this case, "isDone" and "todo" are both expected to be properties on my view object, which is what I want for "isDone", but not for "todo". I'm currently working around this by adding a "todo" property on my view that is equal to a static "todo" string. Is there any way to have a static class attribute when using bindAttr? Fiddle example: http://jsfiddle.net/nes4H/4/ EDIT: We've fixed this

Is there any method to iterate a map with Handlebars.js?

随声附和 提交于 2019-12-03 11:15:06
It is easy to iterate an array with Handlebars.js like: {{#each comments}} <div class="comment"> <h2>{{title}}</h2> {{{{url}}} </div> {{/each}} and an array like: { comments: [ { url: "http://www.yehudakatz.com", title: "Katz Got Your Tongue" }, { url: "http://www.sproutcore.com/block", title: "SproutCore Blog" }, ] } But I don't find a method to iterate a map like: { headers: { "Host": "www.example.com", "Location": "http://www.example.com", ... Much more map items ... } } Is there any method to iterate a map with Handlebars.js? Or I have to restructurate the object like: { headers: [ { key:

event.target is undefined in events

戏子无情 提交于 2019-12-03 11:14:06
How can one use each input values in events ? Hope my below code will explain you well. HTML: <template name="UpdateAge"> {{#each Name}} <div data-action="showPrompt"> <div> Some content </div> <div> <div> Some content </div> </div> <div> Name : {{name}} Age : <input type="text" name="age" value="{{age}}"/> //I need to access age values in event </div> </div> {{/each}} <template> JS: Template.UpdateAge.events({ 'click [data-action="showPrompt"]': function (event, template) { console.log(event.target.age.value); // TypeError: event.target.age.value is undefined } }); I dont know whether my

loading handlebars template asynchronously

时光怂恿深爱的人放手 提交于 2019-12-03 10:48:19
问题 I'm trying to write a function that will give me a compiled handlebars template (I have all my templates in separate files) using an ajax call to get the template and compile it for use, but I need to use a promise so I can actually use it. function getTemplate(name){ $.get('/'+name+'.hbs').success(function(src){ var template = Handlebars.compile(src); //can't return the template here. }); } How do I do this with promises so I can do something like: $("a").click(function(e){ getTemplate('form