Workaround to dynamic includes in Pug/Jade

前端 未结 3 1580
粉色の甜心
粉色の甜心 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:41

    There is no way to do this for now, but you can work out your application architecture without dynamic extends.

    Possible solution #1

    1. Make a layout.jade that conditionally include multiple layouts:

      layout.jade:
      
      if conditionalVariable
          include firstLayout.jade
      else
          include otherLayout
      
    2. In your view, extend layout.jade, and define conditionalVariable in the controller (true/false):

      view.jade:
      
      extends layout
      
      block content
          p here goes my content!
      

    Possible solution #2

    1. Pass configurations to the layout

      - var lang = req.getLocale();
      doctype html
         block modifyLayout
      
    2. split the project into multiple entrances, each entrance extends the layout and passes its different configs, and includes different things in different blocks

      extends ../layout
      block modifyLayout
         - var lang = "en" //force language to be en in this page.
      block body
         include my-page-body
      

    Possible solution #3

    use something like terraform which uses pug as its rendering engine, but it enables you to use dynamic partials like this

    != partial(dynamicFileFromVariable)
    

提交回复
热议问题