Workaround to dynamic includes in Pug/Jade

前端 未结 3 1615
粉色の甜心
粉色の甜心 2021-01-20 23:59

I understand that Pug does not support dynamic includes or extends in templates. Ie

extend path/to/template 

works but not

ex         


        
3条回答
  •  青春惊慌失措
    2021-01-21 00:52

    I had this issue aswell and found this question while searching for a solution. My solution is similar to Nikolay Schambergs Answer, but i thought i should share it.

    I've created a function that renders templates by giving it a path and passed it to the options object. Maybe it helps in your case aswell

    const includeFunc = (pathToPug, options = {}) => {
        return pug.renderFile(pathToPug, options); //render the pug file
    }
    
    const html = pug.renderFile('template.pug', {include: includeFunc});
    

    and then use it as followed in your template:

    body
      h1 Hello World
      |!{include(dynamicPugFilePathFromVariable)}
    

提交回复
热议问题