handlebars.js

Access values using {{#each}} in a one dimensional array

这一生的挚爱 提交于 2019-12-04 15:19:25
问题 I've found a lot of examples of using the {{#each}} helper to iterate over multi-dimensional arrays, but I can't figure out how to access each value in a one-dimensional array. For example, take this array: skills: ['Design', 'Development', 'HTML5', 'CSS', 'JavaScript'], How do I output each item, in a helper like below? template: Handlebars.compile( '<div>' + '{{#each skills}} {{ the_item_output }} {{/each}}' + '</div>' ), What do I need to put in placed of {{ the_item_output }} to see the

Rendering a string array with handlebars

ε祈祈猫儿з 提交于 2019-12-04 14:57:07
问题 Lets say I have an array like this in ember controller, selectedUsers: ["Popeye", "Sulley", "Gru"]; Now, how can i render each users in an unordered list using handlebars? Can i use the {{#Each}} helper? 回答1: Yes, you should use an each loop: <ul> {{#each selectedUsers}} <li>{{ this }}</li> {{/each}} </ul> From the docs: You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over. <ul class="people_list"> {{#each

How do I load two models in one JSON request in Ember-data?

社会主义新天地 提交于 2019-12-04 14:46:38
问题 Using Ember-data and Ember.js, I'm trying to load two models with one JSON request. The models have a relationship analogous to this: App.Person = DS.Model.extend({ name: DS.attr('string'), dogs: DS.hasMany('App.Dog'), }); App.Dog = DS.Model.extend({ name: DS.attr('string'), owner: DS.belongsTo('App.Person'), }); My server is sending JSON like this: { "dog": { "id": 1, "name": "Fido", "owner": { "id": 1, "name": "John Smith", "dogs": [1] } } } …And yet, Ember-data still sends a request (using

JQuery, Ajax, Handlebars clear table then repopulate

半世苍凉 提交于 2019-12-04 14:10:27
Build a form that goes out and grabs data then populates a table using handlebars.js. Works great on the first go around then won't populate again if data is changed. Any ideas on how to get this to work? $('a.button').on("click", function(e){ e.preventDefault(); var date = $('#datepicker').val(); var func = "get_all_swo_by_date"; //$('table.output').empty(); $.ajax({ url: "../includes/db_functions.inc.php", type: "post", data: ({ p : date, f : func }), dataType: "json", success: function(results) { var source = $("#entry-template").html(); var template = Handlebars.compile(source); $('table

Pass object as parameter to onclick method in handlebars template

ε祈祈猫儿з 提交于 2019-12-04 13:33:58
问题 I have a JSON that looks like this: { list: [ { name: 'AAA', id: 1, age: 34 }, { name: 'BBB', id: 2, age: 24 } ] } And a template like this: <ul> {{#each list}} <li onclick="someFunc({{this}})">{{name}} ({{age}}) </li> {{/each}} </ul> Basically I just want to pass the current object , to a function that does something with it. Now if I try it, then the generated HTML just has ... onclick="someFunc( [object Object] )" ... whereas I'd like it to be like this: ... onclick="someFunc( {name: 'AAA'

precompile handlebar templates

こ雲淡風輕ζ 提交于 2019-12-04 12:50:23
问题 I'm wondering what's the best way to save precompiled handlebar templates and include them in an HTML file. The site: http://handlebarsjs.com/precompilation.html simply advises to call handlebars infile -f outfile I put a template in a file and called the command. I got a JS file containing a function program1 How would I now compile a second template? The sample at http://jsperf.com/jquery-template-vs-handlebars contains multiple functions named program[1-9](...) I assume they somehow must

Handlebars custom if helper else is undefined

China☆狼群 提交于 2019-12-04 10:44:19
问题 I have a custom helper, as follows: Handlebars.registerHelper('hasAccess', function(val, fnTrue, fnFalse) { return val > 5 ? fnTrue() : fnFalse(); }); and my template, as follows: {{#hasAccess this.access}} You have access! {{else}} You do not have access {{/hasAccess}} It works, except fnFalse is undefined. So, how am I supposed to render the 'else' branch? 回答1: Handlebars provides custom helpers an object containing the different functions to apply, options.fn and options.inverse . See http

Using @index in meteor #each iterator doesn't work [duplicate]

南楼画角 提交于 2019-12-04 10:30:58
问题 This question already has answers here : How can I get the index of an array in a Meteor template each loop? (6 answers) Closed 4 years ago . why doesn't this work in meteor? https://github.com/wycats/handlebars.js/issues/250 回答1: It's not yet implemented in meteor's version of handlebars; there's a subtlety about the reactivity of @index rendering properly. You can read more about it here: https://github.com/meteor/meteor/issues/489#issuecomment-11270564 回答2: This is definitely a frustration

Handlebars passing object into helper

主宰稳场 提交于 2019-12-04 08:20:47
Currently I have an Ember object that looks like this: name: 'Bob' xs: { 'actual':50 'target':55 } I have around 5-6 fields similar to xs . I need a helper method that can take that xs object and then return whether or not the target has been hit. I thought of doing this: Handlebars.registerHelper('hasHitTarget', function(attribute) { if (attribute.actual >= attribute.target) { return block(this); } }); {{#each user in App.userController}} {{#hasHitTarget user.xs}} Target Hit {{/hasHitTarget}} {{/each}} Everything I've read online says this should work. But it doesn't. When I console.log

Template was precompiled with an older version of Handlebars than the current runtime

大憨熊 提交于 2019-12-04 06:27:37
I have this error, but the different between this question and my question is that I'm using gulp instead grunt. First, my handlebar runtime is handlebars v4.0.5 (js file). The output of handlebar -v is 4.0.5 This is my gulpfile.js : var gulp = require('gulp'); var uglify = require('gulp-uglify'); var handlebars = require('gulp-handlebars'); var wrap = require('gulp-wrap'); var declare = require('gulp-declare'); var concat = require('gulp-concat'); gulp.task('default', ['templates','scripts'], function () { }); gulp.task('templates', function () { gulp.src('templates/*.hbs') .pipe(handlebars()