vol4ok / hogan-express

Mustache template engine for express 3.x. Support partials and layout
MIT License
138 stars 31 forks source link

FIX: lambdas receive the text but don't receive the lctx (variables needed to do useful things) #37

Open spadkins opened 8 years ago

spadkins commented 8 years ago

I declare my lambdas to have two arguments, the enclosed text and the set of options/variables. e.g. See at the bottom for two examples. However, while the "text" is passed ok, the "ctx" is always undefined. In this state, lambdas are much less useful than they could be. I copied the module and made the following change, and it works perfectly for me. May I suggest that the change is made in the sources? (As it is, I am using the modified version of hogan-express.js in my project.)


hogan-express.coffee (line 85) OLD: return lambda(hogan.compile(text).render(lctx)) NEW: return lambda(hogan.compile(text).render(lctx), lctx)


hogan-express.js (line 131) OLD: return lambda(hogan.compile(text).render(lctx)); NEW: return lambda(hogan.compile(text).render(lctx), lctx);


var lambdas = {
    ifLoggedIn: function(text, ctx) {
        var user = null;
        if (ctx && ctx.user) {
            user = ctx.user;
        }
        if (user && user.username !== 'guest') {
            return text;
        }
        else {
            return '';
        }
    },
    ifNotLoggedIn: function(text, ctx) {
        var user = null;
        if (ctx && ctx.user) {
            user = ctx.user;
        }
        if (!(user && user.username !== 'guest')) {
            return text;
        }
        else {
            return '';
        }
    }
};
tandrewnichols commented 7 years ago

@spadkins This module is no longer maintained (as far as I can tell), but I've migrated it here and published it as hogan-xpress and incidentally, this issue is fixed there . . . or should be. If it's not, feel free to open an issue there.