Jade Templates - Dynamically Calling a Mixin

試著忘記壹切 提交于 2019-11-30 21:36:27

This is a feature that is not very obvious in Jade, as it is not explicitly mentioned in the documentation. You can actually use the interpolation syntax (#{...}) for dynamically choosing the mixin name.

From the Jade language guide:

interpolation? yup! both types of text can utilize interpolation, if we passed { name: 'tj', email: 'tj@vision-media.ca' } to the compiled function we can do the following:

#user #{name} <#{email}>

outputs <div id="user">tj &lt;tj@vision-media.ca&gt;</div>

Example usage:

mixin foo(item)
  p Foo called

mixin bar(item)
  p Bar called

mixin twoColumns(obj)
  .container-fluid
    .row(class=obj.class)
      for item in obj.items
        .col-xs-12.col-sm-3
          +#{item.template}(item)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!