Search code examples
javascripthandlebars.js

Pass a partial as a variable


Is it possible to pass a partial as a variable to another partial? Something like:

{{>
template1
image="images/image1.jpg"
content=template2
}}

Solution

  • If I correctly understand what you are asking, then I think your question is answered in the Handlebars documentation on Dynamic Partials:

    If a simple variable has the partial name, it's possible to resolve it via the lookup helper.

    Your template would have to call template1 and pass they dynamic partial name as a string:

    {{> template1 content="template2"}}
    

    Your template1 partial would have to include the following:

    {{> (lookup . "content")}}