Bah! I should really read the docs more carefully.

>From the docs: "*Include:* *Loads a template and renders it with the 
current context.*" 
https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#include

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

onsdag den 13. februar 2019 kl. 15.47.10 UTC+1 skrev Mikkel Kromann:
>
> 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
>
>  
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/811a1421-4d7f-432f-ad1c-95e4f6015af7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to