While rendering templates we often come into situations where data remains the same, but sometimes we want to render it one way, other times - another.

Examples:

a library supporting various CSS frameworks. You would always render panel <https://getbootstrap.com/docs/3.3/components/#panels-heading> title and panel body, but bootstrap does it using one HTML, material uses another.

you may decide to render a piece of data in a panel, input, table cell, etc.

analysis:

There's a huge note with include tag <https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#include> clarifying how include tag is NOT used for purposes listed above. This leads me to believe (but I have not researched it thoroughly) that the question has been asked many times before.

proposal:

implement support for passing HTML renders into an included template.

I see two possible approaches for this:

Approach 1 template example:

   {% includeblock template with ... %}
        {% subblock block1 %}
             HTML code
        {% endsubblock %}
        {% subblock block2 %}
             HTML code
        {% endsubblock %}
   {% endincludeblock %}

block1 and block2 would be blocks defined in template using existing block syntax. I'm not sure if recycling existing block tag would be possible in this context, but I would certainly prefer to avoid new template tag syntax. Basically what this tag would do is first evaluate the subblocks, then override the blocks defined in the template, then render the template.

Approach 2 template example:

   {% block block1 as block1_var %}
       HTML code
   {% endblock %}
   {% subblock block2 %}
       HTML code
   {% endsubblock %}
   {% include template with block1=block1_var block2=block2_var %}

This approach is a bit less structured than the first one, but it requires a lot less modification. Basically it would only add the with parameter to store block render in a variable instead of actually rendering it to output. Possible problem with block inheritance within the template.


Would either of the above be interesting to include in Django templating code?

Thanks for your consideration,
Jure

--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/2b317d14-f8e1-c47d-4883-ff06040d1204%40gmail.com.

Reply via email to