SC_Chupacabra
SC_Chupacabra

Reputation: 14397

Express - Async Templating

I'm using Express-Handlebars for my templating engine and I'd like to make an asynchronous call from within my templates like the following example:

{{{cmsLoader 'search-{$searchTerm}' searchTerm=query.input defaultId='search-default'}}}

This would query the db for a chunk of html by a specific id (i.e. 'search-video-games'), if the key exists, it adds the html. If the key doesn't exist, it looks for the defaultId (i.e. 'search-default') and adds it instead. If the defaultId is not specified or doesn't exist, nothing is added.

Most of the backend functionality is in place. My problem is the doing async processing inside of a helper. Is there a way to wait for the results of an async call from an hbs-helper before finalizing the render?

NOTE:

I realize I can make these calls from inside the controller and feed the data to the view, but there are tons of templates most with multiple CMS blocks and since these will be changing regularly, accounting for them ahead of time would be difficult to maintain.

Upvotes: 0

Views: 1720

Answers (1)

Patrick Steele-Idem
Patrick Steele-Idem

Reputation: 457

Handlebars is not an asynchronous templating engine so that is not possible. You would need to switch to an asynchronous templating engine for Node.js such as Marko. Marko allows you to asynchronously fetch additional data even after rendering has begun.

Upvotes: 1

Related Questions