-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feature suggestion | Synchronous/Asynchronous Helpers #717
Comments
This has come up in the past but we didn't act on it as the use case wasn't clear. Since handlebars still has to wait until all of the data is available to render providing async evaluation is simply a convience for something that the code that generates the context can do in a much more efficient manner. Basically adding async evaluation at this point would be quite expensive both in terms of compatibility and runtime performance that I don't quite see a use case for right now. Do you have a concrete example of what you are trying to do? |
I agree with the performance issues, but I think, it will be better, if we can optionally make it aysnc, for example, RegisterHelper & RegisterHelperAsync, or anything like that. Actually, I came to think about these async handlebars, while i work with node.js. I am working on some applications using express.js & the template engine i use is handlebars. So, If i need to get some value from a database call, while compiling the view, its not possible with this synchronous working, For example, Handlebars.registerHelper('getDbValue', function(id) {
var Model = require('./myModel.js');
Model.getValue(id, function(data){
return data;
});
}); The above example will not work, and returns nothing. The following is my concept. And, I don't know, if it is totally right or it can be implemented or not. Just using a callback function instead of return, in case of async method. Handlebars.registerHelperAsync('getDbValue', function(id, callback) {
var Model = require('./myModel.js');
Model.getValue(id, function(data){
callback(data);
//or
//callback(new Handlebars.SafeString(data)); //in case of safestring.
});
}); I am experiencing more issues like the above one, and I can show more example according to my scenario, if anyone is interested in this feature. Thanks |
@robincsamuel, When including database lookups in view generation, the whole idea of MVC separation goes at the window. I think you are arguing that you might not know you need the data until the view is being rendered, but to me that suggests functionality that should be implemented at the controller level rather than when generating your view. Combined with the performance considerations @kpdecker mentioned, async helpers just seem wrong. -- my 2c |
At this point the stance of the project is that data resolution should be done prior to calling the template. Outside of the business logic in or outside of the template concerns, async resolution is more of utility behavior that other libraries such as async are much better suited at handling. |
I want to comment something. Even though it would be throwing out the window the DB example, this could be really helpful for mutational templates. For example, a template with "subviews" inside, and that you don't want to split into several other templates. I just want to update a part in the view, and have a simple logic for that, instead of either repainting my whole view (flickering effect) or having my controller to build the whole thing for all these "mini views" What do you think? |
@tomasdev I mean that :) |
This feature is made available in node with express if you use https://github.com/barc/express-hbs. However the async version of helpers do not play nicely with subexpressions and a few other edge cases. I would like to see this feature reconsidered for inclusion in handlebars, or at least consider how handlebars core can better support this kind of extension. I believe Ghost demonstrates a clear and valid (although perhaps uncommon) usecase for async helpers, because our view layer is customisable. On the frontend, all of Ghost's templates are provided by the theme. The theme is a very thin layer of handlebars, CSS and client JS, the only data it has access to is that which we provide in advance. It does not have access to a controller or any behaviour changing logic. This is very deliberate. In order to expand out the theme API, we want to start adding helpers which define additional data collections the theme would like to make use of. For example something like:
Ghost has a JSON API which is available both internally and externally. So this fetch query would map to our browse tags function. It doesn't need to use ajax/http to all the endpoint, instead, an async helper can fetch this data from the API internally and carry on as usual. I don't argue that this is a common use case, and I accept it breaks the standard MVC model, but I believe it is valid and useful. |
@ErisDS Great news! And me too don't argue that its common issue, but it helps. |
It's worth noting in this case, that many of the operations we are currently using async helpers for are synchronous under the hood, but they're structured as promises. To give a detailed example... All data in Ghost is accessed via an internal API. This includes global info like settings. Requests to the settings API hit a pre-populated in-memory cache before hitting the database, so we're actually just returning a variable, but by structuring this as a promise it's easy to go off to the database if we need to. It also ensures everything is consistent - otherwise the settings API would be synchronous, and all the other internal data requests would be async, which would make no sense. I know that structuring everything with promises can be quite confusing at first, but it is one of those things that you don't understand how you lived without once you've got it. With generators coming in ES6, support for asynchronous resolution of functions will baked right in to JavaScript - and this similar issue: #141 mentions that it would be nice to make handlebars work with yield. I'm not sure how the coming release of HTMLbars might impact on this, but I think it at least warrants further discussion. |
Ran into another use case while trying to create a helper for acl resolution. This would fit nicely into my templates:
But the actual isAllowed method from node-acl is asynchronous (allowing a database backend for instance). A workaround is to fetch all the user permissions beforehand (allowedPermissions), but that's kinda itchy |
@kpdecker Any further thoughts on these use cases? |
@ErisDS I understand the desire here but I severely doubt this will ever make it into the language in callback or promises form. This is something that is very hard to do cleanly from an API perspective and effectively requires us to rewrite large portions of the template engine to support it. My recommendation is that all of this be handled before the rendering cycle is entered by the upstream model/data source. The yield idea is an interesting one but if someone wanted to take a look at what would be required there, that would be an amazing research project, but browser support for that seems very far off to me and I honestly haven't messed around with any of those features yet on any of my projects. |
Just my "two" (well, couple) cents you might want to consider:
|
I might be able to offer a better example where it would be usefull to have. We work a lot with cordova for mobile applications and need to localize for many languages. Cordova offers functions to help with formatting dates, numbers, currencies and so on. Example: Handlebars.registerHelper('stringToNumber', function(string, type)
{
type = type || 'decimal';
navigator.globalization.stringToNumber(string, function(number)
{
return number;
}, function()
{
return NaN;
}, {
type: type
});
}); This would be awesome to have imo. |
I have found the handlebars-async package on npm. But it's a little older and I don't know if it works with the current Handlebars-version. I have also just written something similar for promises. The package promised-handlebars allows you to return promises from within helpers. I plan to use it in one of my projects, but so far it hasn't been used in a production environment. But there are unit tests for several few edge-cases (such as calling async-helpers from within async block-helpers) and they are all green... |
@nknapp that sounds amazing! express-hbs has async support, and the async works for block helpers, but nesting async helpers does not work - so I'm really interested to see this working - means there's hope yet for express-hbs 👍 |
@ErisDS, do you think I should post it there. I didn't now that |
To add to the valid use use-cases, what if you need to fetch values from a translation DB based on current locale? <div class="howItWorks">
{{{i18nFetch id=how-it-works locale=locale}}}
</div> Moreover, what about adding a CMS block from a DB entry using a dynamic id like: <div class="searchCms">
{{{cmsLoader 'search-{$term}' term=params.input defaultId='search-default'}}}
</div> This is especially useful for server-side rendering (i.e. using express-handlebars). |
Here's another use case: I'm writing a documentation-generator for Swagger (simple-swagger), which allows for external schema definitions. I'd like to write a Handlebars helper that recognizes when a schema is defined externally, goes to the provided URL where that schema lives, retrieves it, and uses that data to render that portion of the Handlebars template. If I had to retrieve this data before calling Handlebars' compile method, I would have to recursively iterate through a JSON document whose structure I don't know in advance, find all the instances of external schemas, retrieve them, and insert them into the JSON. Basically, anytime a Handlebars template is used to render JSON schema data (json-schema.org), an async render method would be useful, because JSON schema always allows subparts of a schema to be defined externally. |
@dwhieb have you had a look at bootprint-swagger for the documentation-generator? It's almost what you describe (except that external schemas are not yet implemented, but that would be a great feature). If you have any feedback, please open an issue there. And, I think promised-handlebars works quite well with async helpers. |
I have a use case where being able to use promises in helpers would be useful. I'm using handlebars to generate the HTML for my blog. In order to build valid structured data for each article, I need to get the dimensions I'm using for the article image. Right now, I'm doing it like this:
The I'll look into using promised-handlebars and express-hbs but I think the ability to use promises in helper functions would be a great addition to Handlebars! |
FWIW, I've been doing a lot of asynchronous HTML rendering using Hyperscript, |
going to give express-hbs a try - but I think in 2018 its a strange thing not to support. I know that there is a purist view that async stuff should not be done as part of the view but instead in some magical thing called a controller (as if MVC is somehow unarguably "correct") - but that's a little short sighted for two key reasons a) external libraries - most people now are writing entirely with async/await - I think in my code more than 9 out of 10 functions are async... in some cases "just in case". Not supporting an async function means all async libraries are suddenly completely inaccessible b) generic controller functions. I would argue that something like this: {{#query "select name, total from summary"}}
<tr><td>{{this.name}}</td><td>{{this.total}}</td></tr>
{{/query}} is shorter, cleaner, easier to maintain, and basically superior in every conceivable way when compared with having a bespoke controller function that sticks that stuff into a variable and passes it into a template, which the template then has to know about and access. |
I see nothing's changed for the last 8 years? With everything and their mother getting full |
@Atulin Honestly, I don't think this is going to happen soon, too. It may seem like a big project, but from 2016 to 2020, I was basically the only one who did active maintenance. Now, @jaylinski has taken over. I wanted to support im, but in the last months, I had other things to do in my free time... So he is more or less alone in this. What I want to say: But there is no such thing as a "active development team" or any kind of "planning". All we have done in the last years is answer to questions, fix bugs, especially security issues. Unless this changes, there will be no native async support for Handlebars. |
Gotcha. I found a package that adds async helper support and it's working well, so I'll say "good enough" |
Do you mean this? https://www.npmjs.com/package/promised-handlebars |
https://github.com/gastonrobledo/handlebars-async-helpers I did check the one you linked, but it hasn't been updated for half a decade, and the examples use this weird |
@Atulin if I was to submit a pull request based on some of the ideas in @gastonrobledo's project, is there a chance of you or @jaylinski accepting/merging it? |
I'm not a maintainer of Hbs, so it's not me you should be asking lol |
@CheyenneForbes yes, there is a chance that we will accept an async-helpers-improvement (but only in version 5). |
This should very much be supported. You won't need it 90% of the time sure, but you desperately need it when you do. |
@Atulin About https://www.npmjs.com/package/promised-handlebars: I know, there hasn't been an update for over 6 years. And I am not actively working on it anymore. On the other hand, it has almost no dependencies and no known security issues as well. And it's a small library with a tight use-case so there is actually no need for an update imho. Handlebars has not had a real update for years as well. When there is a major version bump I will make sure that promised-handlebars stays compatible. If anyone wants to make a PR updating the documentation to remove I am not promising fast responses here. But if there is something critical, Handlebars maintainers can reach me through faster channels. In case an actively maintained version for |
In any case. Of course it would be more powerful to have it included in Handlebars directly. But I don't see the resources for implementing this. |
if performance is the issue why not throttle it using required pagination and limit the data returned? |
Registering a helper as Sync or Async, It helps to listen for callbacks, and get the data from callback.
The text was updated successfully, but these errors were encountered: