admin管理员组文章数量:1315810
Is it possible to setup a jade conditional in my layout template based on page path? Is there a page path variable? I am looking to do things like:
if page = "/about"
link(rel='stylesheet', href='css/about')
else
link(rel='stylesheet', href='css/main')
Where this is part of the layout template. Where and how do I define the variables to drive this?
Is it possible to setup a jade conditional in my layout template based on page path? Is there a page path variable? I am looking to do things like:
if page = "/about"
link(rel='stylesheet', href='css/about')
else
link(rel='stylesheet', href='css/main')
Where this is part of the layout template. Where and how do I define the variables to drive this?
Share Improve this question asked Apr 24, 2015 at 21:45 user2022284user2022284 4431 gold badge9 silver badges24 bronze badges3 Answers
Reset to default 5Yes, it is possible:
- var page = window.location.pathname
if page === "/about"
link(rel='stylesheet', href='css/about.css')
else
link(rel='stylesheet', href='css/main.css')
Please note that above snippet assumes the template is executed in browser environment. If this is not the case, you can get the pathname using the language that you are using and pass it to the template as a datum. As an example, if you are using the Express framework, path
property of the request
object returns the path part of the URL.
I would set the path dynamically with req.path in express:
res.locals.path = req.path;
res.render('about');
and then using path in jade file:
if path === "/about/"
link(rel='stylesheet', href='css/about.css')
else
link(rel='stylesheet', href='css/main.css')
Depends on your setup
In express:
res.locals.cssPath = 'css/about.css';
res.render('about');
Jade: Not sure about the syntax here since i don't use jade but you have access to locals
link(rel='stylesheet', href=cssPath)
本文标签: javascriptJade conditional based on page urlStack Overflow
版权声明:本文标题:javascript - Jade conditional based on page url? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741990277a2408970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论