添加额外的类来渲染部分
我正在寻找有关部分的帮助。我已经为我的网站上的菜单创建了一个部分,但我想知道是否可以向部分添加一个额外的类。更具体地说,我希望为 li
菜单对象分配一个附加类,以便我可以指示用户位于该特定页面上。
这是我的部分代码(我正在使用 HAML)。它名为 _menu.html.haml,位于“共享”文件夹中。
.three.columns
%ul#nav
%li
%a{:href => "../pages/about"} About
%li
%a{:href => "../pages/careers"} Careers
%li
%a{:href => "../pages/contact"} Contact Us
%li
我的 carrers.html.haml 页面代码如下:
code...
= render :partial => 'shared/menu'
more code...
我想将 .active_page
类添加到位于部分中的职业链接。此类更改文本后面的背景图像以指示用户位于特定页面上。更具体地说,我希望它看起来像这样:
%li.active_page
%a{:href => "../pages/careers"} Careers
是否可以使用部分来做到这一点?
I'm looking for some help regarding partials. I have created a partial for a menu on my site, but I would like to know whether I can add an additional class to a partial. More specifically, I am looking to assign an additional class to an li
menu object so that I can indicate that the user is on that particular page.
Here is my code for the partial (I'm using HAML). It's named _menu.html.haml and it is located in the 'shared' folder.
.three.columns
%ul#nav
%li
%a{:href => "../pages/about"} About
%li
%a{:href => "../pages/careers"} Careers
%li
%a{:href => "../pages/contact"} Contact Us
%li
My code for the carrers.html.haml page is as follows:
code...
= render :partial => 'shared/menu'
more code...
I would like to add a .active_page
class to the careers link located in the partial. This class changes the background image behind the text to indicate that the user is on a specific page. More specifically, I'd like it to look like so:
%li.active_page
%a{:href => "../pages/careers"} Careers
Is it possible to do this using partials?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用部分的事实不会改变您使用 HAML、ERB 或类似内容的方式。如果您想添加一个类,可以,只需将
.active_page
添加到 HTML 标记即可。如果这是在 HAML 中执行的有效操作,那么您可以在视图中的任何位置执行此操作,即使它恰好是部分操作。部分只是模块化/可重用的视图。就像您可以在实例方法和类方法内部使用代码一样,因为它们只是代码,所以您可以在部分视图和“常规”视图中使用相同的 HAML 代码。
The fact that you're using a partial doesn't change how you're able to use HAML, or ERB, or anything like that. If you want to add a class, yes, then just add
.active_page
on to an HTML tag. If it's a valid thing to do in HAML, then you can do it anywhere in a view, even if it happens to be a partial.A partial is just modularized/reusable views. Just like you can use code inside an instance method and inside a class method, because they're just code, so can you use the same HAML code in a partial and in a "regular" view.
有很多方法可以做到这一点。我不会使用部分,我会这样做:
假设 @active_page 已由您的控制器创建。
There are a bunch of ways to do this. I would not use partials, I would do:
Assuming @active_page has been created by your controller.