node.js - jade template conditional class nodejs expressjs -


i have jade template file header , uses bootstrap markup. depending on page user on, navigation bar needs add class .active nav item. best way avoiding long code this.

header.jade

if nav=='home'    li.active       a(href="/") home else    li       a(href='/') home if nav=='about'    li.active       a(href='/about') else    li       a(href='/about') 

route

router.get('/about', function(req, res) {    res.render('about', { nav:'about' }); }); 

notice how if there many more links in header, longer. there better method add class 'active' page being viewed?

thanks tyler

you create mixin handles rendering of list item. way logic code not have repeated:

mixin header-item(name, url)   if name.tolowercase() == nav     li.active       a(href=url)= name   else     li       a(href=url)= name  +header-item('home', '/') +header-item('about', '/about') 

Comments

Popular posts from this blog

rdbms - what exactly the undo information lives in oracle? -

bash - How do you programmatically add a bats test? -

clojure - 'get' replacement that throws exception on not found? -