Passing Pug variables from child template for use in parent template block

44 Views Asked by At

I'm working on a project that utilizes the Pug template engine. All my Pug files (layout.pug, head.pug, index.pug, etc.) are stored within a folder named pug. In my layout.pug file, I've established the foundational structure of my HTML document. My aim is to include the head.pug file to dynamically set the page title and canonical URL.

However, I've encountered an issue where the head.pug file is not being detected or applied during the rendering process. I've meticulously reviewed the file locations, syntax, and indentation, yet the problem persists.

What are some troubleshooting steps I can take to resolve this issue?

here is the code:

1) head.pug

meta(charset="UTF-8")
meta(name="viewport" content="width=device-width, initial-scale=1.0")
block title
 title #{pageTitle}
block description
 meta(name="description" content="#{pageDescription}")
block canonical
 link(rel="canonical" href="#{canonicalURL}")enter code here

2) Layout.pug

doctype HTML
html(lang="en")
 block head
  include head.pug
 body
   include navbar.pug
   block content
   include footer.pug

3) index.pug

extends layout

block head
    include head.pug
        block title
            - var pageTitle = "My Website"
        block description
            - var description = "This is my official website"
        block canonical
            - var canonicalURL = "https://adityasutar.com/"

block content
    h1 Hello

Meta tags are not detected in the browsers

1

There are 1 best solutions below

0
Sean On

There's a few things wrong with the code:

  1. include declarations can't have things indented beneath them.
  2. A block in a child template will replace the content of a block with the same name in a parent template
  3. There's no <head> element defined (note that naming a block "head" doesn't automatically render a <head> element)
  4. Attribute interpolation syntax in the form (attr="#{variable}") doesn't work in Pug anymore

A better way to achieve what you're trying to do would be as follows:

  • layout.pug
    doctype html
    html(lang='en')
      head
        block head
          include head.pug
      body
        include navbar.pug
        block content
        include footer.pug
    
  • head.pug: Note the updated interpolation syntax for attributes.
    meta(charset='UTF-8')
    meta(name='viewport', content='width=device-width, initial-scale=1.0')
    title #{pageTitle}
    meta(name='description', content= pageDescription)
    link(rel='canonical', href= canonicalURL)
    
  • index.pug: Note the use of prepend instead of block here. This will put this block's content before any content within the corresponding block in the parent template, instead of replacing it. A separate block for each variable isn't necessary either.
    extends layout.pug
    
    prepend head
      - const pageTitle = "My Website"
      - const description = "This is my official website"
      - const canonicalURL = "https://example.com/"
    
    append content
      h1 Hello