hogan-express
hogan-express copied to clipboard
Dynamic partials
There should be a way to provide directly the partial string, instead of loading it from the filesystem.
Fe. I have case where the partial is requested elsewhere, but I can't then just give the straight-up HTML it produces to hogan-express. Just plain variables aren't really a possiblity either 'cos then the child partials wont be rendered. :/
Or any given case with dynamic partial would require functionality as such.
After thinking about this a bit, I guess the easiest solution would be to provide the (string) partial as a function, since otherwise it would be rather hard to figure out if the string is path or template without doing filesystem lookup, and still it wouldn't necessarily be clear since you might want to replace a static partial with dynamic.
At simplest it could be just a typeof path === 'function'
check.
if(typeof path === 'function') {
path = path.call(ctx); // Or what ever context is the right one to pass over
} else {
// Load file
...and then do...
res.render('testpage', {partials: {
fncPartial: function() { return dynamicallyCreatedPartial; }
}});
...with this you could have dynamic partials without breaking anything with backwards support. It wouldn't be too intrusive to the codebase either.
Of course it would be even better if the function partial would just use callback (like the readfile does), so that you could do more complex things with it as well.
Another solution would be to do partial lookup after rendering, so that you could pass in partial as variable and it would still have it's child-partials rendered. Though it might have quite performance hit since things are effectively parsed twice.
The dynamic partials should probably not be cached at render-engine level as well, since they are... well, dynamic. Let the user worry about the caching is one is applicable.
Is there a specific tool that has been used for generating the .coffee file from .js file?
js => coffee: http://js2.coffee/ coffee => js: https://mikethedj4.github.io/Coffee2JS/