I've had this issue and have used {% with %} or moved the my.bonnet() call
into the view/context.    I agree, not ideal, but I was never moved to make
it better in general.

If you're suggesting a general caching layer in the template such that a
given expression is only called once in the course of a template render
(including inheritance, includes, etc.) then I think you would need a way
of whitelisting which expressions were without side effects (similar to
is_safe w/ autoescaping).  It seems like a fair bit of book-keeping, but it
could clearly be added in a backwards-compatible way.




On Sun, Jun 2, 2013 at 2:36 PM, Daniele Procida <[email protected]> wrote:

> The for ... empty pattern in templates is common and useful: <
> https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for-empty>
>
> But this is another common pattern:
>
> {% if my_bonnet.bees %}
>     <ul>
>         {% for bee in my_bonnet.bees %}
>             <li>{{ bee }}
>             ...
>
> In other words, we have to check first for items in the iterable before
> creating a <ul> or whatever other furniture to put them in.
>
> The problem here is that my_bonnet.bees gets asked for twice, and it could
> be my.bonnet(), some very expensive function.
>
> One solution is to put everything inside:
>
> {% with my_bonnet.bees as bees %}
>
> but now:
>
> * we've used up a precious level of indentation
> * we've introduced a new variable in the templates to worry about
> * it just feels a bit fussy
>
> Is this enough of an issue to make it worthwhile implementing some other
> approach in the template system?
>
> Daniele
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers" 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 http://groups.google.com/group/django-developers?hl=en
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" 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 http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to