with/include template pattern? seeking best practices

2012-09-04 Thread Ivan Kirigin
I find myself writing things like this a lot in django templates

{% for e in some_list %}
{% with e as element %}
{% include "single_element.html" %}
{% endwith %} 
{% enfor %}

This would be in a template which shows a lists of elements, with another 
template that renders a single one. The template single_element.html uses 
element locally. I find this encapsulates my templates well, especially in 
making single_element.html far more reusable.

I've been doing this for a while and wanted to know if there is a better 
way I just don't know about. Also, I don't know the performance 
implications of so many with/include calls. This list could be very long 
(100s or 1000s).

Thanks!
Ivan Kirigin
http://kirigin.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/48yhZIb9P9MJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: with/include template pattern? seeking best practices

2012-09-05 Thread e.generalov
{% for e in some_list %}
{% include "single_element.html" with element=e %}
{% enfor %}


I find myself writing things like this a lot in django templates
>
> {% for e in some_list %}
> {% with e as element %}
> {% include "single_element.html" %}
> {% endwith %} 
> {% enfor %}
>
> This would be in a template which shows a lists of elements, with another 
> template that renders a single one. The template single_element.html uses 
> element locally. I find this encapsulates my templates well, especially 
> in making single_element.html far more reusable.
>
> I've been doing this for a while and wanted to know if there is a better 
> way I just don't know about. Also, I don't know the performance 
> implications of so many with/include calls. This list could be very long 
> (100s or 1000s).
>
> Thanks!
> Ivan Kirigin
> http://kirigin.com
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/lV9pUIZXyPoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: with/include template pattern? seeking best practices

2012-09-05 Thread Mike S
The code `e.generalov` gave has been valid since Django 2.3: 


For more complicated context, you can also write an inclusion tag <
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags>
 
and use that to render the inner template.

On Tuesday, September 4, 2012 6:21:47 PM UTC-4, Ivan Kirigin wrote:
>
> I find myself writing things like this a lot in django templates
>
> {% for e in some_list %}
> {% with e as element %}
> {% include "single_element.html" %}
> {% endwith %} 
> {% enfor %}
>
> This would be in a template which shows a lists of elements, with another 
> template that renders a single one. The template single_element.html uses 
> element locally. I find this encapsulates my templates well, especially 
> in making single_element.html far more reusable.
>
> I've been doing this for a while and wanted to know if there is a better 
> way I just don't know about. Also, I don't know the performance 
> implications of so many with/include calls. This list could be very long 
> (100s or 1000s).
>
> Thanks!
> Ivan Kirigin
> http://kirigin.com
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/aeoIyYajiZcJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.