agconti
agconti

Reputation: 18123

Django: making {% block "div" %} conditional with a conditional {% extends %}

I would like to share a template between AJAX and regualr HTTP calls, the only difference is that one template needs to extend base.html html, while the other dose not.

I can use

{% extends request.is_ajax|yesno:"app/base_ajax.html,app/base.html" %}

To dynamically decide when to extend, but I also need to include {% block 'some_div' %}{% endbock %} tags to tell the renderer where to put my content. The ajax call needs those tags to be left out because jQuery tells it where to put the content with $('somediv').html(response).

Any idea on how to dynamically include those block tags when its not an ajax call?

I've been referencing this question to figure it out:

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

Attempt to make it work through an {% if %}:

{% extends request.is_ajax|yesno:",stamped/home.html" %}
{% if request.is_ajax == False%}
{% block results %}
{% endif %}
{% load stamped_custom_tags %}

...
Content
...
{% if request.is_ajax == False%}
{% endblock %}
{% endif %}

but this fails when parser runs into the {% endif %}

Upvotes: 0

Views: 846

Answers (2)

agconti
agconti

Reputation: 18123

My Solution:

{% extends x|yesno:"stamped/blank.html,stamped/home.html" %}

Where blank.html contains:

{% block results %}{% endblock %}

<!-- to allow for corrected shared rendering 
    with ajax posts and normal django rendering -->

and home.html is my standard app page with a results block to extend.

Upvotes: 0

Rob L
Rob L

Reputation: 3734

Wouldn't this, or some variant, do?

{% if request.is_ajax %}
    {# ajax! #}
{% else %}
    {% block 'some_div' %}{% endbock %}
{% endif %}

Upvotes: 1

Related Questions