templatespugmaster-pages

JADE pug template engine nested layout


is there any way to have multiple nested layouts in JADE?

for example: master.pug then master-spec.pug then homepage.pug

what I'm trying to achieve - is to add a canonical meta tag to each page in the application, while the master.pug layout is auto-generated, I can't edit it. The only solution I see - is to use an intermediate layout template, like master-c.pug that extends the master.pug and after that each page in the app gonna extend my intermediate layout master-c.pug (instead of the original read-only master.pug)

for instace:

master.pug (that I can't edit)

doctype html
html
  head
    block head

then the master-c.pug

extends master.pug

block head
  link(rel='canonical', href=locals.canonicalUrl)

then the actual page:

extends master-c.pug

block head
  ...

however it doesn't work like that. The canonical link is completely ignored. why?


Solution

  • By default block is replacing parent's block, so if you write block head without defining content, canonical url defined in parent will be gone.

    So, use append or prepend to keep it:

    extends master-c.pug
    
    append head
      ...
    

    see: Block append / prepend