Jade Templates - Dynamically Calling a Mixin

前端 未结 1 1257
梦谈多话
梦谈多话 2021-01-06 04:12

How can I use a string from the json being fed into a Jade template to dynamically load a mixin? Below, the goal is for twoColumn.jade to load the foo

相关标签:
1条回答
  • 2021-01-06 05:01

    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)
    
    0 讨论(0)
提交回复
热议问题