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:
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