Jade - 调用页面特定的 css 页面
我有页面特定的 css 文件,我想自动调用。有人有优雅地做到这一点的好方法吗?
I have page specific css files I would like to call automatically. Does anyone have a nice way of doing this elegantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到,
在渲染玉模板时将
req
(请求对象)作为局部变量传递(或者甚至只是将req.path
作为传递)路径
)。这可以在您的layout.jade 中处理,它将适用于您的每个路线路径。如果您想变得更奇特,您可以建立一个一致的模式,其中页面的路由将 1 对 1 映射到公共目录中 .css 文件的文件系统路径。在这种情况下,您可以轻松地将样式表链接标记放入条件条件中,并且仅在磁盘上找到匹配的文件时才链接到 .css 文件。
This should do it
Where you pass either
req
(the request object) as a local variable when rendering the jade template (or even just pass inreq.path
aspath
). This could just be handled in your layout.jade and it will work for each of your route paths.If you want to get fancy, you could establish a consistent pattern where a page's route maps 1 to 1 to a filesystem path for a .css file in your public directory. In that case you could easily but the stylesheet link tag inside a conditional and only link to the .css file if you find a matching one on disk.