Dynamic Handlebars Partials

Yesterday I learned you can use lookup to render dynamic partials in Handlebars.

Say you have a modal partial inside which you need to render different content which is too complex to pass as an arguments.

You can use the lookup method and pass it the variable that contains the name of the partial you want to use in each respective instance:

{{#if modalContent }}
    {{> (lookup . 'modalContent') }}

Now when inserting the modal you can use the modalContent argument to pass it the partial to be rendered as content:

{{> modal modalContent="modal-image-text" }}

Leave a Reply