X

Jade Include & Template Inheritance Part 6

Jade

Jade Node.js Template Engine, Include & Template Inheritance Part 6

In this post, I will write about using includes and template inheritance in Jade template engine.
You can also check out my previous 5 post on Jade.

html
    head
        title Hello Includes
        include style.css
        style(type="text/css")
           include style2.css
    body
        p Two css were added
        include footer.jade
        include data/copyright.html

One thing to note is the scope in the parent file and the include file is shared, so it allows one to use variables inside the include that are defined outside. Also one has to use the right extension for the file with the correct path. The example above shows the data/copyright.html is an html file located in the data folder.

There are also some other ways to include, css and javascript code blocks in Jade.

//include the jquery with script tag
script(type='text/javascript' src='jquery.min.js')

//code inside of script tag
script(type='text/javascript').
    if (typeof username !== "undefined") {
        alert(username);
    }

//include with link tag for css
link(rel='stylesheet', href='style.css')

//style inside of style tag
style(type='text/css').
    #content {
        font: 12px Arial;
    }

Template Inheritance

Most sites have a generic look and feel, and if you are using includes (partials) it could soon become very ugly and hard to maintain all the includes. This is where Jade provides us with template inheritance, using the Block keyword as placeholders to inject other templates into the view.

We may have already seen this if we have used express web engine the layout.jade file as show below

//layout.jade
doctype html
html
  head
    title= title
    block stylesheets
      link(rel='stylesheet', href='/stylesheets/style.css')
    
    block jscripts
        script(src="jquery.min.js")
  body
    block content
    block footer

In order to use this layout file, Jade provides us with the keyword extends, for example in our index.jade file we can extend the layout like.

extends layout

block content
  h1= title
  p Welcome to #{title}

block footer
  Copyright © 2014

From the above example, we see that we are replacing content, but Jade also provides us with prepend and append to the blocks. For example, if we wish to add zepto.js to our layout we can use the append keyword.

extends layout

append jscripts
  script(src="zepto.js")

block content
  h1= title
  p Welcome to #{title}

block footer
  Copyright © 2014

Or if we want to prepend a css file to the link tag, so that style2.css loads before style.css we can use the prepend keyword.

extends layout

prepend stylesheets
  link(rel='stylesheet', href='/stylesheets/style2.css')

block content
  h1= title
  p Welcome to #{title}

block footer
  Copyright © 2014

Note: Conditional statements like if else do not work with blocks since they are evaluated during compile time. The code below does not work.

if(loadLogin)
  extends loginlayout
else
  extends layout

Summary

This marks the end of the Jade, node.js template engine series, if you have any questions feel free to contact me, in the upcoming post I will write more about node and grunt/gulp (the Javascript task runner/ stream building system).

Sign up and follow my mail list if you wish to be posted on upcoming blog post.

Categories: Node node.js
Taswar Bhatti:

View Comments (1)

  • Thanks you Taswar for shedding some light on this somewhat obscure language. I have added some valuable notes to my collection!

Related Post