Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Helper alias feature? #348

Open
estliberitas opened this Issue Oct 23, 2012 · 3 comments

Comments

Projects
None yet
4 participants

Hello to everyone. I thought to offer helper alias feature. I really do not know if it useful, so want to discuss.

For example in Ember we have unbound helper:

{{unbound x.y.z}} 

Maybe it would be better to create a helper alias * (indirection operator from world of C)

Ember.Handlebars.helperAlias('unbound', '*');
{{* x.y.z}}

Another way - using regexp replacement, so short transcription:

{{div."class1 class2 class3" MyView}}

is translated to:

{{view App.MyView tagName="div" class="class1 class2 class3"}}

The purpose of such feature is decreasing size of resulting template. From the other side, right now I do such job now in my web app before calling Handlebars.compile() and have no problems. 😄

Owner

wycats commented Oct 23, 2012

It sounds like what you want is some kind of macro feature? This is an open issue already but would require some design work. I believe @dmarcotte has previously worked on and/or considered this.

I would be open to some use-case-driven exploration of the feature.

Contributor

dmarcotte commented Oct 24, 2012

Hrm... wasn't me. But I can give it some thought now :)

Seems like this can be implemented without modify Handlebars itself (though {{div."class1 class2 class3" MyView}}, while a pretty neat syntax idea, has no helper identified and isn't Handlebars syntax, so some compromises would need to be made).

Something like this should take care of the base aliasing (and can probably be enhanced to partially hit the regex scenario):

var createAlias = function(alias, helper) {
        Handlebars.registerHelper(alias, function() {
            return helper.apply(this, arguments);
        });
    };

createAlias("ifAlias", Handlebars.helpers.if);
createAlias("eachAlias", Handlebars.helpers.each);
//    ... etc...

Disclaimer: whipped this together quickly. Apologies in advance for any glaring pitfalls.

As for use cases... I'm not sure I'd be inclined to obfuscate templates just to save a few bytes, but some sort of Handlebars minimizer tool based on this idea might be cool (@estliberitas is that what you're doing in your webapp?)

(Side note: you won't be able to use * as an alias because identifiers have to be alphanumeric or one of _, $, -. Just wanted to mention it so that if you try the above with * and it doesn't work, you'll know why.)

is that what you're doing in your webapp?

Yes, web app processes templates this way too before they go to client.

some sort of Handlebars minimizer tool based on this idea might be cool

👍 Yep, I hope in some time I will push such tool!

@kpdecker kpdecker added this to the Backlog milestone Jul 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment