8

I want to make sure I have understood this correctly before I change all my templates. I have a base.html with my overall layout. This has a {% block content %}. Each of my content pages extends "base.html" and is surrounded by the block tag. This works great. My view returns the rendered content page and it's placed nicely in the layout.

Now I have also created a menu bar in menubar.html The menu should be the same for every page except that the color of the selected page/content is different. So it needs to know what content got loaded.

My base.html now also has a {% block menubar %}

I am about to open up all of my content templates and add the following to them:

{% include "menubar.html" %}

Then, in menubar.html, I will surround the menu with block tags. Is that correct? I thought the point of the block system was that somehow things could be controlled more from the base.html, without changing all my templates.

2 Answers 2

7

I'd suggest just putting {% include "menubar.html" %} into base.html.

To highlight the current page in your menu use something like this: https://stackoverflow.com/a/477719/473285

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

2 Comments

looking at it now. looks good except I can even get {{ request.path }} to show- And using context_instance=RequestContext(request). Will post a question on that.
This worked great. I followed the referenced article 110j.wordpress.com/2009/01/25/… and the article that it referenced. I did have to give the naming parameter as name="home_url_name" for it to work.
3

If your content templates extends from base.html, and the {% block menubar %} is outside the {% block content %} you don't need to modify all the contents templates, just the base.html

3 Comments

But how does the menu get loaded? I return my rendered content page. This extends base.html. But how does base know what to put inside the menu tag?
Inside the {% block menubar %} you put the {% include "menubar.html" %}, so the template system will put inside that block whatever inside the menubar.html file. Thus if you need some context information in order to render your menu, you could simply pass it in context dict to the render_to_template function for instance.
Then you don't even need the block. Just use include. My issue then is passing the context info. Looking into answer below, thanks!

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.