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

Partial application of templates? #1053

platinumazure opened this Issue Apr 6, 2013 · 1 comment


None yet
2 participants

I was wondering if there was any interest in being able to partially render/apply a template function. By this, I mean render as many variables as possible, and if not all parameters are supplied, to return a new template function that only needs the missing variables.

Here's an example of what I mean, using _.partialTemplate as a function name:

unrenderedTemplate = _.partialTemplate("<%= greeting %>, <%= name %>!");

helloTemplate = unrenderedTemplate({ greeting: "Hello" });
// The value of helloTemplate is roughly equivalent to
// _.template("Hello, <%= name %>!").

helloPlatinumAzureString = helloTemplate({ name: "Platinum Azure" });
// => "Hello, Platinum Azure!"

As a bonus, maybe these partial templates could also include metadata on which parameters have already been applied (and with what values), and which parameters still need application in order to return a string.

I'm happy to take an initial stab at it, but does anyone see a massive problem with the idea even at this early stage? Or am I missing something in the existing source code allowing for this?



jashkenas commented Apr 7, 2013

It's not a great idea for Underscore, but it would make for an awesome templating library in its own right. Go for it!

@jashkenas jashkenas closed this Apr 7, 2013

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