开发者

Django conditional template inheritance

开发者 https://www.devze.com 2022-12-25 09:14 出处:网络
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 hyperl

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 st开发者_如何学Goatement 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?


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.


From here: Any way to make {% extends '...' %} conditional? - Django

It can be done like this :

{% extends ajax|yesno:"ajax_base.html,main_base.html" %}

Or:

{% extends a_variable_containing_base_template_name %}

Which ever best suited for you.

Regards;

0

精彩评论

暂无评论...
验证码 换一张
取 消