Hi group
I considered posting this to django-dev or opening a ticket but
decided to erron the side of caution.
I've been looking thoroughly through django's template code the last
two daysbecause I'd like to render only parts of templates.
Pseudocode:
    tpl = Template("""        {% block someblock %}foo{% endblock %}
     {% block otherblock %}bar{% endblock %}    """)        node =
tpl.get_block("someblock")    result =
node.render(template.Context({}))
The motivation to do this is to get rid of the overhead that goes into
templates when dealing with reasonably AJAX heavy and complex
templates. Thinkof a template structure like this:
    <!-- base.html -->    {% block body %}        {% block map %}{%
endblock %}        {% block content %}{% endblock %}            {%
endblock %}        <!-- map.html -->    {% extends "body.html" %}
{% block map %}<div id="map" />{% endblock %}    {% block content %}
     {{ object_list|unordered_list }}    {% endblock %}        <!--
pin.html -->    {% extends "map.html" %}    {% block content %}{{
object }}{% endblock %}
Ideally when requesting the page that serves pin.html via AJAX we'd
want to serve only the content that is in {% block content %}. One way
to do this
    <!-- pin.html -->    {% extends "map.html" %}    {% block content
%}{% include "pin-detail.html" %}{% endblock %}
And the corresponding view:
    def pin(request, id):        #...        if request.is_ajax():
        tpl = loader.get_template("pin-detail.html")
content = json.dumps(content = tpl.render(Context({'object': pin})))
         return HttpResponse(content, mimetype = 'application/json')
     return render_to_response('pin.html', {'object': pin})
This gets very tedious with time and isn't desireable.
So I came up with a mixin that gets rid of the repetition. It checks
if the request is AJAX'd and if so, renders all the blocks listed in
`self.partials`and `json.dump`'s the result to the HttpResponse. It
all works, except:
* {{ block.super }} does not work without resolving the whole chain of
parent  templates and building up the quite complex corresponding
context. It's   basically a copy of the first half of the
`django.template.loader_tags.ExtendsNode.render` method.
* Rendering blocks that are part of a parent template but not the
current one   fails because of the complexity of how the context for
the current and its  parent blocks are created. I figure building up
the context properly is   duplicating too much of django's internals
to be sane work.
Ignoring these two points, it all works very well - but I'd also like
to be able to render blocks that do make use of {{ block.super }} and
that can bedefined at any point in the template chain. Ideally the
`BlockNode`, `ExtendsNode` and `Template` classes would provide some
methods to walk the chain and build the context.
Are you guys interested in having similar functionality? It would
involve somerefactoring of the template internals and probably some
decisions of the corecomitters - I wouldn't really want to change the
`Template` class because itis the interface to adopt for third party
template languages.
Also, here's more pseudo code to illustrutate how the partial mixin
looks whenused.
    class PinView(PartialRenderingView):        template_name =
'pin.html'        # Blocks to render and dump as json when doing AJAX
requests         partials = ['title', 'pin', 'form']
def get(request, id):            # ...             return
self.render_to_response(request, pin = pin, form = form)
 def post(request, id):            # ...            return
self.render_to_response(request, pin = pin, form = form)

Anyway. Thoughts?

Alen

-- 
http://twitter.com/flashingpumpkin
http://github.com/flashingpumpkin

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to