On Fri, Jul 18, 2014 at 4:45 AM, Neto <paulosouzamac...@gmail.com> wrote:

> Using 'locals ()' would slow page loading? What is better?
>

Using locals() as template context might give you a *slight* slowdown in
template rendering (since you'll have lots of context variables that you
won't be using). However, that effect would be negligible.

There's a much better reason that you shouldn't use locals() as a template
context - it can lead to unexpected side effects and hard-to-debug problems.

If you use locals() as a template context, *every* variable in your local
context - including ones that you've included for counting or other
temporary storage - are used as part of your template rendering. So - lets
say you develop a template that checks {% if foo %}. Intially; your view
doesn't assign anything to foo, so the block doesn't execute. But then, six
months later, you start using a temporary variable foo in your view - all
of a sudden, the template will start rendering the block. You haven't
changed anything in your template - you're just using a temporary variable
that you weren't using before. You didn't *intend* to make a change to your
template; the rendering of the template has changed as result of something
unrelated to the template design.

The Zen of Python says "explicit is better than implicit"; this is a
perfect example of just that. If you're explicit about what you want to
render, and what you pass to a context, this sort of bug will never happen.

Yours,
Russ Magee %-)

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJxq84-k_UJqHifWbgwaXrbymL9Dg43H5cM3A3R%2B_XsfiJTzew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to