Skip to content
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

Open
uniquejava opened this issue Dec 16, 2015 · 5 comments
Open

handlebarsjs #8

uniquejava opened this issue Dec 16, 2015 · 5 comments

Comments

@uniquejava
Copy link
Owner

uniquejava commented Dec 16, 2015

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.

 Handlebars.registerHelper("debug", function(param1, param2) {
        console.log("<<<<<<<<<<<  debug start");
        console.log("=========  this start ========");
        console.log(this);
        console.log("=========  this end ==========");

        if (!_.isUndefined(param1)) {
            console.log("param1 =>", JSON.stringify(param1));
        } else {
            console.log("param1 is undefined:", param1);
        }

        if (!_.isUndefined(param2)) {
            console.log("param2 =>", JSON.stringify(param2));
        } else {
            console.log("param2 is undefined:", param2);
        }

        console.log(">>>>>>>>>>>>  debug end");

    });

You could then use that helper in any template like:

{{debug}}

or like:

{{debug someValue}}
@uniquejava
Copy link
Owner Author

handlebars.runtime不包含Handlebars.compile函数, 这个函数接受一段文本做为模块(不是路径),可以用text!插件载入这段文件,我想用hbs!插件,但是这个插件死活不识别模板中注册的handlebars helper.(待解决)

@uniquejava
Copy link
Owner Author

数组下标用@index, 取第0个元素用{{array.[0]}} , 使用变量{{array.[{{@index}}]}}

{{#each array}}
    {{@index}}: {{this}}
{{/each}}

For object iteration, use @key instead:

{{#each object}}
    {{@key}}: {{this}}
{{/each}} 

@uniquejava
Copy link
Owner Author

eq用法

{{#eq moduleName "Search"}}
{{else}}
{{/eq}}

@uniquejava
Copy link
Owner Author

整合expressjs

examples here:
https://github.com/donpark/hbs

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

@uniquejava
Copy link
Owner Author

访问root context
{{@root.obj.xx}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant