I'm currently creating my new website with express. The page is split up in different "sections" (for example a blog, information about my project and so on). I'd like a have a "sidebare" next to my content thats different for each section. For example in the "blog"-Section id like to have a list of my tags or categorys and in the project section there should be a list of all projects. The content depends on the section and is the same for every "subpart" of this section.
One solution would be to create a function that creates the sidebars content and call this function in every function that handles a route. But I don't like this solution because it doesn't seems to "be the rig开发者_JS百科ht one"...
Does anyone have a nice solution for this?
If I understand your question correctly, you want to know how to render multiple 'sections' using the same overall template, yes? Each section could differ depending on the content on the page and the main URL.
I can suggest two ways: 1) IMHO the right way - Define a base template (say index.jade if you're using jade/express) and you would create routes for say a homepage (/) and a blog page (/blog) using that:
# Home Page
app.get '/', (req, res) ->
# Do some simple DB transactions, etc, and get the main area content.
res.render 'index.jade', { json: json }
# Blog Page
app.get '/blog/:id', (req, res) ->
blog.grabPost req.params.id, (json) -> # Grab content of the blog post from the DB
res.render 'index.jade', { json: json }
Next, create a few 'partial' URL's for the remainder of the content:
# Sidebar Partial
app.get '/sidebar/:page/:id', (req, res) ->
sidebar.grabContent req.params.id, (json) ->
res.render '{#req.params.page}.jade', { json: json }
You would then call the partial client-side upon pageload via JQuery like so:
$ ->
$('.sidebar').load '/sidebar/blog/365' + id, (response, status, xhr) ->
2) Using Express 'view partials' which I'm much less familiar with. Documentation can be found here: http://expressjs.com/guide.html#view-partials
From the description: "The Express view system has built-in support for partials and collections, which are “mini” views representing a document fragment. For example rather than iterating in a view to display comments, we could use partial collection:"
精彩评论