在Jade中从子模板中设置页面标题

时间:2022-12-04 23:11:59

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:

我想通过jade在布局的子模板中设置我的页面标题。我不想在路由中设置它们,因为这需要服务器重新启动。以下是我希望实现的目标:

layout.jade:

layout.jade:

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

child.jade:

child.jade:

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

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

任何关于我如何能做到这一点的想法都是很有帮助的。

Thanks!

谢谢!

2 个解决方案

#1


16  

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

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

layout.jade

layout.jade

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

child.jade:

child.jade:

block variables
  title = "ST"
extends layout

#2


6  

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

我的逻辑非常简单,因为上面的答案对我不起作用:

in layout.jade

在layout.jade

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

in child.jade:

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

在这个解决方案中,存在的布局将检查一个变量titleVar:如果真的存在(这并不等于零),那么布局使用titleVar的值设置为标题,否则,预定义的名称(在我们的例子中:“这个标题设置的布局!”)的布局文件。您可以自己尝试一下,并从子模板中注释// titleVar的定义,并查看结果。我希望这个解决方案可以帮助其他人:)

#1


16  

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

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

layout.jade

layout.jade

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

child.jade:

child.jade:

block variables
  title = "ST"
extends layout

#2


6  

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

我的逻辑非常简单,因为上面的答案对我不起作用:

in layout.jade

在layout.jade

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

in child.jade:

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

在这个解决方案中,存在的布局将检查一个变量titleVar:如果真的存在(这并不等于零),那么布局使用titleVar的值设置为标题,否则,预定义的名称(在我们的例子中:“这个标题设置的布局!”)的布局文件。您可以自己尝试一下,并从子模板中注释// titleVar的定义,并查看结果。我希望这个解决方案可以帮助其他人:)