Pass arguments to Jade/Pug templates

1k Views Asked by At

I'm trying to pass the title variable into the head section of the _layout.pug file. i can't see any way to achieve this neatly so have had to resort to pre-pending variables to the block:

//- _layout.pug
html
    head
        block headStuff
            title #{title}
//- main.pug
extends _layout

prepend headStuff
    -var title = 'The Positioning Schema'

Am I attacking this the wrong way? Should this be a mixin?

1

There are 1 best solutions below

3
On

Normally, things like the title would be passed through as variables from whatever back end it's hooked up to (node, usually for me). If you're just using it to compile straight to static HTML without a back end, though, you should just be able to pass it to the layout in block head

layout.pug

html
  head
    //standard head stuff that applies to all pages
    block head
  body
    //generic layout stuff
    block content

main.pug

extends layout
block head
  title The main page

block content
  h1 This is the main page content
  p And more of it.