Merric Mercer wrote:
> I have  view that returns a whole bunch of different variables and 
> boolean flags.    The state of these flags determine what the user 
> actually sees on their screen.
> 
>  From a design perspective I seem to have two options:-
> 
> 
> 1.   Design a template with lots of {% if %} conditional statements to 
> check the status of various flags, in order to display the appropriate 
> information to the user.
> 
> OR
> 
> 2.   Do the bulk of the work in the view,  by doing something like:-
[snip]
> 3.  Do a combination of the two above
> 
> Option number 2 lacks the flexibility of doing stuff in the template, 
> but would it make a big difference in speed?  I am concerned that having 
> a lot of conditional statements in the template might be slower than 
> doing the work in the view.
> 
> Can anybody shed some light on this and the trade off (if any) of speed 
> versus flexibility.

A view is Python code, which is compiled into bytecode and then executed 
by the Python interpreter.  Whereas a template is interpreted by the 
Python template engine, which is Python code executed by the Python 
interpreter.

A template will will always have that extra layer of interpretation. 
Think of it as a new language with a compiler written in Python.  (Which 
it is...)  So from the smallest perspective, an if-else in a template 
will always be slower than the equivalent {%if%}-{%else%}-{%endif%} in a 
view.

But before you pull out a stopwatch and move all of your application's 
decisions into the views, think about the long-term maintainability, 
likelihood of bugs, the goodness of separating of "what" code from "how" 
code, etc.  The history of software is littered with code that was 
extremely efficient but impossible to maintain or adapt to changing 
requirements.  You can double your application's performance by buying 
or leasing a new box + more memory next year.  What % improvement do you 
think code shifting from template to view will get you?

John


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to