Answer the question
In order to leave comments, you need to log in
How to correctly make a menu in jade?
Do not tell me how to correctly implement the menu
Example:
there are 3 pages
home.jade
about.jade
contact.jade
each includes include menu.jade
menu should be of this type (li should have an active class when we are on its page and an empty link as in the first li Home):
ul.nav
li.nav-item.active
a(class='link', href='/') Home
li.nav-item
a(class='link', href='about.html') About
span.label test
li.nav-item
a(class='link', href='contact.html') Contact
Answer the question
In order to leave comments, you need to log in
Implementing this sort of thing is Jade's forte
. Include is not a good fit for such tasks. Use inheritance. In the parent template, define all common blocks, in the child templates, leave only what changes
1. Create a parent template, define a content block and a block of common variables, remove duplication in the mix, call this mix in a cycle, take out the data for the cycle separately:
//- filename: root.jade
- var nav = [{url: '/', name: 'Home'}, {url: 'about.html', name: 'About'}....];
doctype html
html(lang="en")
block sharedVars
head
block title
body
ul.nav
each i in nav
+navItem(i.url, i.name, i.url == pageUrl)
block content
mixin navItem(url, name, current)
li.nav-item(class={active: current})
a.link(href=url)= name
extends root
append sharedVars
- var pageUrl = '/';
block title
| My title
block content
h1 Мой заголовок
.content
p Hello World
I'll tell you how I do it.
The menu items themselves are in a separate json file (a lot of info is stored this way)
[
{
"title": "Главная",
"url": "home.html"
},
{
"title": "Каталог",
"url": "catalog.html"
},
{
"title": "Акции",
"url": "actions.html"
}
]
mixin nav(data, current)
nav.nav&attributes(attributes)
if(data && data.length)
ul.nav__list
each item, index in data
li.nav__item.nav-item(class=(index === current ? "nav-item--current" : undefined))
a(href=item.url).nav-item__link
span.nav-item__title #{item.title}
extends layouts/_default
block head
- pageTitle = 'Заголовок страницы';
block header
- currentNav = 1;
block content
doctype html
html(lang="ru-RU" class="no-js")
include ../parts/_head
body
block header
include ../parts/_header
.content#content
block content
block footer
include ../parts/_footer
It seems to me that then you need to handle the header with handles, that is, durble and transfer to each page and not make
the header a module (this question worries me, I'm waiting for an answer)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question