简体   繁体   English

Node.js, Express, EJS - 导航中当前页面上的活动 class

[英]Node.js, Express, EJS - Active class on current page in navigation

I'd like to render a 'current' class on each of my navigation links depending on which page I'm on in my layout.ejs template.我想根据我在 layout.ejs 模板中的哪个页面在每个导航链接上呈现“当前”class。

Currently, my express controller index looks like this:目前,我的快递 controller 索引如下所示:

// About
exports.about = function(req, res) {
    res.render('content/about.ejs', {
        title: 'About'
    });
};

And in my layout.ejs I have the following, which I'd like be rendered dynamically.在我的 layout.ejs 中,我有以下内容,我希望动态呈现。

<ul class="nav" id="nav">
    <li><a href="/">Home</a></li>
    <li class="current"><a href="/about">About</a></li>
    <li><a href="/">Contact</a></li>
</ul>

Any ideas of how to achieve this?关于如何实现这一目标的任何想法?

You could include a page_name: 'about' in the res.render data and then in the template something like:您可以在res.render数据中包含page_name: 'about' ,然后在模板中包含以下内容:

<li <% if (page_name === 'about') { %>class="current" <% } %> ><a href="/about">About</a></li>

I didn't test the syntax, but that's the gist.我没有测试语法,但这就是要点。

You can pass a variable to the page like this, and use a condition inside the class attribute.您可以像这样将变量传递给页面,并在类属性中使用条件。

<a
    class="nav-link <%= page_name === 'home' && 'active' %>"
    href="#"
>
    Home
</a>

@thataustin's answer is correct! @thataustin 的回答是正确的! but I lost the default class, so I found a way to keep the default class when it's not active.但是我丢失了默认类,所以我找到了一种方法来在它不活动时保留默认类。 Hope it helps someone someday希望有一天它可以帮助某人

<li            
    <% if (page_name === 'about') { %>
        class="nav-item current"
        <%} else { %>
        class="nav-item"
        <% } %> >
    <a href="/about">About</a>
</li>

Firstly, I have passed an object {navSelectTitle : "your-required-page-to-be-selected"} while rendering each page in the main app.js .首先,我在渲染主app.js中的每个页面时传递了一个对象{navSelectTitle : "your-required-page-to-be-selected"} Then, you can create a list in your ejs template like following:然后,您可以在 ejs 模板中创建一个列表,如下所示:

<% const navItems = ["home", "articles", "videos", "audios"] %>

Then, you can loop through the list and add a class to make the link appear selected.然后,您可以遍历列表并添加一个类以使链接显示为选中状态。 (Here, I have used the class class="selected" to make the link appear selected) (在这里,我使用 class class="selected"使链接显示为选中状态)

Also, since I need my home page to have the href as href="/" and not <a href="/home" , I have created a separate if statement for the case of "home" to accomodate this special case.另外,由于我需要我的主页将 href 作为href="/"而不是<a href="/home" ,所以我为“home”的情况创建了一个单独的 if 语句来适应这种特殊情况。

<% navItems.forEach(navlink => { %>
    <% if(navlink == navSelectTitle) { %>
        <% if (navlink == "home") {%>
            <li class="links selected"><a href="/"><%= navlink %></a></li>
        <% } else {%>
            <li class="links selected"><a href="/<%= navlink %>"><%= navlink %></a></li>
        <% } %>
                
    <% } else { %>
        <% if (navlink == "home") {%>
            <li class="links"><a href="/"><%= navlink %></a></li>
        <% } else {%>
            <li class="links"><a href="/<%= navlink %>"><%= navlink %></a></li>
        <% } %>
    <% } %>
<% }) %>

The code logic goes as follows:代码逻辑如下:

  1. Loop through the list.循环遍历列表。
  2. If the list item equals to the object value that you passed while rendering the page, then the li tag will have the class="selected" .如果列表项等于您在呈现页面时传递的对象值,则li标记将具有class="selected" If not, the else statement will create a normal link without the selected class.如果没有,else 语句将创建一个没有选定类的普通链接。
  3. To accomodate the case where the home link should have href="/" , there's a nested if-else statement inside the if statement for the said case.为了适应 home 链接应该有href="/"的情况,在上述情况的 if 语句中有一个嵌套的 if-else 语句。

NOTE: Using partial for the navigation bar can help dry your code and makes this logic a little convenient.注意:在导航栏上使用 partial 可以帮助干燥您的代码并使这个逻辑变得有点方便。

[update] - It is also possible to use a ternary logical: [更新] - 也可以使用三元逻辑:

  1. First: Allow access to any variable/data in your views by setting the "server.js" file like so:首先:通过设置“server.js”文件,允许访问视图中的任何变量/数据,如下所示:

app.get('*', (req, res, next) => { res.locals = ({ req: req }); next(); });

  1. Second: then in your frontend, use this ternary syntax you link, ex.:第二:然后在您的前端,使用您链接的这种三元语法,例如:

<li class="nav-item <%- ( req.url.search('about') == 1? ' active ': ' ' ) %> ">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM