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!
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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With