在 Express 中将菜单渲染为 Jade 视图部分
我无法概念化如何使用 Jade 视图部分在 Express 应用程序中渲染具有活动状态的菜单。
我有这样的事情:
ul.menu
li
a(href='/some/route').active
li
a(href='/another/route')
我想要的是将 .active
类应用于当前路由,以便我的 CSS 可以呈现活动的 UI 状态。
当我用 PHP 编写时,这可以通过一些复杂的 if/else 语句链来完成,但我假设有一种更优雅的方法。我缺少什么?
I'm having trouble conceptualizing how to render menus with active states in an Express app using Jade view partials.
I have something like this:
ul.menu
li
a(href='/some/route').active
li
a(href='/another/route')
What I'd like is for the .active
class to be applied to the current route, so my CSS can render an active UI state.
When I wrote in PHP, this would've been accomplished through some complex chain of if/else
, statements, but I'm assuming there's a more elegant approach. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的解决方案与 danmactough 的类似,但我认为更简洁。
我使用中间件将 url 保存为所有请求的本地变量。确保它出现在您的路线之前。
我的 Jade 菜单模板如下所示
My solution is similar to danmactough's, but I think a little tidier.
I use middleware to save the url as a local variable on all requests. Make sure this comes before your routes.
My Jade template for a menu looks like this
杰德对路线一无所知。我认为没有任何方法可以避免某种形式的 if/else。
在 Express 中,只需将路线作为本地传递即可:
在视图中,有一系列菜单链接并执行以下操作:
Jade is ignorant about the route. I don't think there's any way to avoid the if/else in one form or another.
In Express, just pass the route as a local:
And in the view, have an array of menu links and do something like: