2

Basically I want to build a site wide navigation bar that contains links as specified in the local variables in express.js

So I build my list of links I want to include on a page:

class NavigationLink
  constructor: (@title,@url) ->

app.use (req,res,next) ->
  res.locals.navigationLinks = [
    new NavigationLink "About", "/about"
    new NavigationLink "Projects", "/projects"
    new NavigationLink "Skills", "/skills"
    new NavigationLink "Contact", "/contact"
  ]
  next()

Then if I hit the home page:

app.get '/', (req,res) ->
  res.render('about')

Jade tries to render this layout file, which is supposed to loop through the navigationLinks array and render for each one:

!!! 5
html
include header
body
    .container
        .row
            .span3
                ul.nav.nav-tabs.nav-stacked
                    for navigationLink in navigationLinks
                        include navigationLink
            .span9
                block content

However I am getting a reference error 'navigationLinks is not defined' which I assume to mean to mean the locals aren't getting passed through. Would love advice on how to fix the problem, or another design paradigm that is better suited for this kind of thing (dynamic building of links on every page of the site). Thanks!

1
  • 1
    I'm guessing your app.use is below app.router. Commented Aug 25, 2012 at 4:16

1 Answer 1

3

Jonathan Ong is correct. Your code above should work in the default middleware order which would run all the app.use middleware before the app.router middleware. Thus I would agree that elsewhere in your code you probably have a line app.use app.router that comes before the app.use in your code snippet.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so I have verified that the code I had before works when I put app.router below it. Peter's original code using app.locals also works. Convention wise, which way is better? App.locals or setting res.locals like I do above?
res.locals makes more sense for data that is specific to this request like res.locals.currentTemp in your /weather route. app.locals is better for things that most or all of your views will need. In your case, if most of your views use the same layout, it seems appropriate to use app.locals.navigationLinks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.