Set page title from a child template in Jade

ぃ、小莉子 提交于 2019-12-18 14:48:53

问题


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!


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/15709020/set-page-title-from-a-child-template-in-jade

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