views:

163

answers:

2

I've got a CMS that takes some dynamic content and renders it using a standard template. However I am now using template tags in the dynamic content itself so I have to do a render_to_string and then pass the results of that as a context variable to render_to_response. This seems wasteful.

What's a better way to do this?

+2  A: 

"This seems wasteful" Why does it seem that way?

Every template is a mix of tags and text. In your case some block of text has already been visited by a template engine. So what? Once it's been transformed it's just text and passes through the next template engine very, very quickly.

Do you have specific performance problems? Are you not meeting your transaction throughput requirements? Is there a specific problem?

Is the code too complex? Is it hard to maintain? Does it break all the time?

I think your solution is adequate. I'm not sure template tags in dynamic content is good from a debugging point of view, but from a basic "template rendering" point of view, it is fine.

S.Lott
Good point. Maybe this is a case of 'premature optimization'...
andybak
Exactly right, on both counts (seems like a poor design, but not for performance reasons). +1
Carl Meyer
A: 

What you're doing sounds fine, but the question could be asked: Why not put the templatetag references directly in your template instead of manually rendering them?

<div>
    {% if object matches some criteria %}
        {% render_type1_object object %}
    {% else %}
        {% render_type2_object object %}
    {% endif %
    ... etc ...
</div>

Or, better yet, have one central templatetag for rendering an object (or list of objects), which encapsulates the mapping of object types to templatetags. Then all of your templates simply reference the one templatetag, with no type-knowledge necessary in the templates themselves.

The key being that you're moving knowledge of how to render individual objects out of your views.

Daniel