Using variables for a partial template

前端 未结 2 2025
礼貌的吻别
礼貌的吻别 2021-02-05 09:39

I\'m definitely missing something about the way Handlebars works. I need to call different partials depending on the value of a variable. Currently the only way I\'ve found to d

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 10:33

    Update for 2016: Version 3 of handlebars added Dynamic Partials. From the docs:

    It's possible to dynamically select the partial to be executed by using sub expression syntax.

    {{> (whichPartial) }}
    

    Will evaluate whichPartial and then render the partial whose name is returned by this function.

    Subexpressions do not resolve variables, so whichPartial must be a function. If a simple variable has the partial name, it's possible to resolve it via the lookup helper.

     {{> (lookup . 'myVariable') }}
    

提交回复
热议问题