views:

250

answers:

1

I have template that displays object elements with hyperlinks to other parts of my site. I have another function that displays past versions of the same object. In this display, I don't want the hyperlinks.

I'm under the assumption that I can't dynamically switch off the hyperlinks, so I've included both versions in the same template. I use an if statement to either display the hyperlinked version or the plain text version. I prefer to keep them in the same template because if I need to change the format of one, it will be easy to apply it to the other right there.

The template extends framework.html. Framework has a breadcrumb system and it extends base.html. Base has a simple top menu system.

So here's my dilemma. When viewing the standard hyperlink data, I want to see the top menu and the breadcrumbs. But when viewing the past version plain text data, I only want the data, no menu, no breadcrumbs. I'm unsure if this is possible given my current design. I tried having framework inherit the primary template so that I could choose to call either framework (and display the breadcrumbs), or the template itself, thus skipping the breadcrumbs, but I want framework.html available for other templates as well. If framework.html extends a specific template, I lose the ability to display it in other templates.

I tried writing an if statement that would display a the top_menu block and the nav_menu block from base.html and framework.html respectively. This would overwrite their blocks and allow me to turn off those elements conditional on the if. Unfortunately, it doesn't appear to be conditional; if the block elements are in the template at all, surrounded by an if or not, I lose the menus.

I thought about using {% include %} to pick up the breadcrumbs and a split out top menu. In that case though, I'll have to include it all the time. No more inheritance. Is this the best option given my requirement?

+1  A: 

You can put your hyperlinks inside a block that is overridden by the loading templates.

Let's say you have your framework.html like this:

{% extends "base.html" %}

<html>...<body>...
{% block hyperlinks %}
your hyperlinks here
{% endblock %}
rest of the code
</body></html>

You can then create something of a nolinks.html template and use it

{% extends "framework.html" %}
{# here you'll have everything from framework
but now we disable the breadcrumbs #}
{% block hyperlinks %}{% endblock %}

If you're getting the past data you can then use nolinks to render instead of framework.

I hope this helps.

Miguel Ventura
I really like this idea. It gets me part of the way there. The problem is that I would still need a "nolinks" page for every template I needed to disable the menus on. Because the nolinks page extends a specific template, it only links to one at time. I tried including the nolinks page instead of calling it directly, but it appears that including doesn't pick up on the block tags that disable the necessary sections
Ed
My idea was to have a single nolinks page, just with the definition of the empty block. Then all your templates without links could `extend nolinks` instead of extending `framework`. Should you find yourself doubling the number of templates to do this, then instead try passing a variable to the template and use something like `{% if dontshowlinks %}{% block hyperlinks %}{% endblock %}{% endif %}`.
Miguel Ventura
I don't think you can make block tags conditional. That was my original problem. I ended up passing a variable and including the menus or not based on the resolution of that conditional.
Ed