Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set page title from a child template in Jade

I'm wanting to set my page titles in the child templates of the layout via jade. I don't want to set them in the routes since that requires a server restart. Here's what I'm hoping to accomplish:

layout.jade:

!!! 5
  head
    - var title = title || "Default Title Here"
    title #{title}
    // ...

child.jade:

- var title = "Child Title Here"
extends layout
// ...

Any thoughts on how I can accomplish this would be a great help.

Thanks!

like image 390
Dan Avatar asked Mar 29 '13 18:03

Dan


2 Answers

From https://github.com/visionmedia/jade/issues/654#issuecomment-5859502

layout.jade

block variables
!!! 5
head
 - var title = title || "Default Title Here"
 title #{title}

child.jade:

block variables
  title = "ST"
extends layout
like image 81
schaitanya Avatar answered Nov 12 '22 08:11

schaitanya


I ended up with a very simple logic since the above answer did not work for me:

in layout.jade

block head
  - var theTitle = titleVar ? titleVar : "This title was set from The Layout!"
title #{theTitle}

in child.jade:

extends layout
block head
   - var titleVar = "This title was set from the child!"

In this solution, the layout will check for the existence of a variable called titleVar: If it does exist (and it's not equal to zero) then layout uses titleVar's value to set as the title, otherwise, the predefined title (in our case: "This title was set from the Layout!") from the layout file will take place. Try it for yourself and comment // the definition of titleVar from the child template and see the results.
I hope this solution can help others :)

like image 23
Zack S Avatar answered Nov 12 '22 07:11

Zack S