Set page title from a child template in Jade

百般思念 提交于 2019-11-30 11:43:16

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
Zack S

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!