Using {% include %} and getting context with me

20 views
Skip to first unread message

Mikkel Kromann

unread,
Feb 13, 2019, 9:47:10 AM2/13/19
to Django users
Hi.

I have a collection of apps where I find repeating myself when rendering tables in the templates.
In the stylised example below I have three types of tables, foo, bar and baz.
Each app is supposed to render various combinations of tables from its own models, but also from other models' tables.
(yes, it is a hideously complex structure - and believe me, I've tried to simplify it a lot - even with some success)

Can I - in a page which extends a base, include various "template snippets", so that I can reuse the template snippets across several apps?

base.html:
<html>
...
</html>

my_page1.html:
{% extends "base.html" %}

{% block foo %}
{% include foo.html %}
{% endblock % }

{% block bar %}
{% include bar.html %}
{% endblock % }

{% block baz %}
{% include baz.html %}
{% endblock % }

foo.html:
{% if foo_context %}
<!-- do stuff with foo -->
{% endif %}

bar.html:
{% if bar_context %}
<!-- do stuff with bar -->
{% endif %}

baz.html:
{% if baz_context %}
<!-- do stuff with baz -->
{% endif %}

As I understand, the snippets are evaluated without the context of my_page1.html, right (e.g. foo_context).
The docs (https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#include) seem to suggest that I could work around the context problem by writing:

{% include foo.html with foo_context=foo_context %}


Is that correctly understood in this case?


thanks, Mikkel

 

Avraham Serour

unread,
Feb 13, 2019, 9:58:37 AM2/13/19
to django-users
Maybe you can solve this using a templatetag?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8b0eca46-1d00-4bdd-8aba-d8d3a2043a1b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikkel Kromann

unread,
Feb 14, 2019, 12:24:32 AM2/14/19
to Django users
Bah! I should really read the docs more carefully.

From the docs: "Include: Loads a template and renders it with the current context."

The example I posted actually works perfectly fine with regards to the include.
I just needed to fix spelling errors in the {% block %} names.
And in my_page1.html I also forgot the block from the base.html.
A working example of base.html and my_page1.html ought to look like this.

base.html:
<html><body>
{% block content %}
{% endblock %}
<body></html>

my_page1.html:
{% extends "base.html" %}
{% block % content %}

{% block foo %}
{% include foo.html %}
{% endblock foo % }


{% block bar %}
{% include bar.html %}
{% endblock bar % }


{% block baz %}
{% include baz.html %}
{% endblock baz % }

{% endblock content % }

Sorry. Keep calm, carry on :)

Mikkel
Reply all
Reply to author
Forward
0 new messages