1

When using Django templates, should I have some templates that act like "subroutines", so to speak, or should I generate HTML from within my code in these cases?

For example, I have a template with several lists of names, each of which I want to turn into a select. Should I have a template that renders the name_list variable into a select, and do something like this:

#in the view:
return {'name_list_1': name_list_1, 
 'name_list_2': name_list_2, 
 'name_list_3': name_list_3}
#in the template:
{% with name_list_1 as name_list %}
 {% include "sub_name_list_select.html" %}
{% endwith %}
{% with name_list_2 as name_list %}
 {% include "sub_name_list_select.html" %}
{% endwith %}
{% with name_list_3 as name_list %}
 {% include "sub_name_list_select.html" %}
{% endwith %}

Or should I have a function in my code, name_list_to_select_html, which does the same job, and do this:

return {'name_list_1_html': name_list_to_select_html(name_list_1), 
 'name_list_2_html': name_list_to_select_html(name_list_2), 
 'name_list_3_html': name_list_to_select_html(name_list_3)}
#in the template:
{{ name_list_1_html|safe }}
{{ name_list_2_html|safe }}
{{ name_list_3_html|safe }}

Or are both of these wrong and I am getting the philosophy totally wrong?

Additional question: in terms of speed, is it slow to constantly include templates? Is that a bonus point for the in-code html generation?

asked Jul 19, 2012 at 18:03
4
  • stackoverflow.com/faq#dontask. This might be more appropriate some place like codereview.stackexchange.com Commented Jul 19, 2012 at 20:26
  • which particular criteria does this question fall under? it seems pretty straightforward to me: how do django developers accomplish this task? Commented Jul 19, 2012 at 21:20
  • It's a subjective question. The bulleted list merely provides examples of common subjective questions, but if you're asking what people recommend, your question is subjective and not appropriate for StackOverflow. Like I said, the Code Review site would be okay because the entire nature of that site is to gain subjective opinions. Commented Jul 19, 2012 at 21:29
  • @ChrisPratt: ah ok, i think you're right. voting to close the question. Commented Jul 20, 2012 at 3:12

1 Answer 1

3

Generally, HTML should only be generated in the templating system or directly related code. That keeps the view of the data completely separate from the business and functional logic. I feel that's a proper separation of concerns. Go with your first solution.

As for performance, Django should probably take around the same amount of time running either code. But it has built-in view and template fragment caching if you know those segments of code don't need to be regenerated on every request.

answered Jul 19, 2012 at 18:18
Sign up to request clarification or add additional context in comments.

3 Comments

is my first solution the commonly-accepted way to pass variables to templates, or is there something more elegant? something irks me about not being able to call a template like a function.
also, due to the limited nature of django templating, i am invariably forced to have the view of the data interfere with the python code, as i have to return data structures amenable to the templating system. for example, if i want to pass a dict, it's hard to get the templating system to sort it, so i end up having to turn it into a list of items and do the sorting on the view code. isn't having the views in views.py enough separation?
Views gather the data to present (in an easy-to-template way), but I believe all HTML should be done directly at the template level. When I require more python functionality to write HTML, I create custom tags and filters. You also have the option of using a different templating system (although I've never tried it myself).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.