Use a variable in a Jade include

前端 未结 5 1976
暖寄归人
暖寄归人 2020-11-27 14:22

I\'m working with Jade and Express and I would like to use a variable in my include statement. For example:

app.js

app.get(\'/admin\', function (req,         


        
5条回答
  •  遥遥无期
    2020-11-27 14:57

    It's 2019 and using variables in Pug (previously Jade) mixins has become simple.

    When creating your mixin, you can give it parameters as per value(s) you're expecting to pass to the mixin. You can access any nested values using dot notation.

    mixinFile.pug:

    mixin myMixin(parameter1, parameter2, parameter3)
        h2.MyHeading #{parameter1}
        p.MyParagraph #{parameter2.myVariable}
        .MyBox(id= parameter3.id)
    

    index.pug:

    include mixinFile
    block content
        +MyMixin(variable1, variable2, variable3)
    

    You can read more in the official Pug documentation on Mixins.

提交回复
热议问题