Reference error: "Tags is not defined"

I have manipulated the code in this application, and put in a sidebar in the views/layout.jade file. In the sidebar I've cut/pasted the code for handling tags from views/index.jade:

    ul.tags
        each tag in tags
            a(href='/tag/' + tag._id.toHexString())
                li.tag= tag.name

The tags are fetched from the db as they should, and rendered out in the sidebar, however when I click on one of them (which should show a new page with all the posts tagged with the tag) I get an error message:

500 ReferenceError: ... layout.jade:10 8| p 9| ul.tags > 10| each tag in tags 11|   
  a(href='/tag/' + tag._id.toHexString()) 12| li.tag= tag.name tags is not defined

Here's the function that's called in the file "tagcontroller.js" (that's handling the logic for the tags):

app.get('/tag/:id', function(req, res){

    model.Tag.findById(req.params.id, function (err, tag){
        if (err) {
            console.log(err);
            // do something
        }

        var query = blogModel.BlogPost.find( { _id: { $in : tag.blogs } } );
        query.where('date').lte(new Date());

        query.exec(function (err, blogs) {
            if (err) {
                console.log(err);
                // do something
            }

            var query = model.Tag.find({});

            query.exec(function (err, tags) {
                if (err) {
                    console.log(err);
                    // do something
                }
                res.render('tags/detail', { title: tag.name, blogs: blogs, tagList: tags, dateFormatter: dateFormatter });  
            }); 
        });
    });
});

If I put back the code into views/index.jade, everything works fine. But I need to have it in layout.jade to be able to show it on every page, not only on Home (index.jade).

I would really appreciate some help!

Right now, you have:

res.render('tags/detail', { title: tag.name, blogs: blogs, tagList: tags, dateFormatter: dateFormatter });

But if you are doing a each tag in tags, then you should have something like

{ title: tag.name, blogs: blogs, tags: tags, dateFormatter: dateFormatter }