-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handlebarsjs #8
Comments
handlebars.runtime不包含Handlebars.compile函数, 这个函数接受一段文本做为模块(不是路径),可以用text!插件载入这段文件,我想用hbs!插件,但是这个插件死活不识别模板中注册的handlebars helper.(待解决) |
数组下标用@index, 取第0个元素用{{array.[0]}} , 使用变量{{array.[{{@index}}]}} {{#each array}}
{{@index}}: {{this}}
{{/each}}
For object iteration, use @key instead:
{{#each object}}
{{@key}}: {{this}}
{{/each}} |
eq用法{{#eq moduleName "Search"}}
{{else}}
{{/eq}} |
整合expressjsexamples here: my code: var express = require('express');
var hbs = require('hbs');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./src/routes/index');
var users = require('./src/routes/users');
var auth = require('./src/routes/auth');
var app = express();
hbs.registerHelper('eq', function (v1, v2, options) {
if (v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
});
hbs.registerHelper('forEach', function (arr, options) {
var ret = "";
if (arr) {
var offset = parseInt(options.hash.offset) || 0;
var limit = parseInt(options.hash.limit) || (arr.length - offset);
var end = Math.min(offset + limit, arr.length);
for (var i = offset, j = end; i < j; i++) {
ret = ret + options.fn(arr[i]);
}
}
return ret;
});
hbs.registerPartials(__dirname + '/views/partials');
// view engine setup
app.set('views', path.join(__dirname, 'src/views'));
app.set('view engine', 'hbs'); http://stackoverflow.com/questions/8059914/express-js-hbs-module-register-partials-from-hbs-file |
访问root context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The debug Helper
It can be easy at times to get confused about where you are in the stack in a Handlebars.js template. I usually keep a debug helper around to help me figure that out.
You could then use that helper in any template like:
or like:
The text was updated successfully, but these errors were encountered: