Re: Walking the template / Partial rendering

2011-12-09 Thread Alen Mujezinovic
Also, here's the actual code that is used by the mixin to assemble the 
response:

https://gist.github.com/70beb521546d59deb207

Compare this to what is going on in  `ExtendsNode.render`:

https://code.djangoproject.com/browser/django/trunk/django/template/loader_tags.py#L103

The code in the gist does handle `{{ block.super }}` but it comes with the 
cost of duplicating a big part of the render method. Having the context 
assembly moved out of the render method in the `ExtendsNode` would already 
be a big step :)

Alen

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/vUL82_HQ6QIJ.
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.



Re: Walking the template / Partial rendering

2011-12-09 Thread Alen Mujezinovic
Hey Tom

I've had a look through previous tickets if something similar came up and 
also looked at the link you posted. It's the right direction, but not quite 
what I'm trying. Pulling out a single block from a template and rendering 
is easy. It breaks and throws an `AttributeError` though as soon as you've 
got a `{{ block.super }}` in the block you've pulled from the template due 
to the context not being fully assembled:

http://dpaste.com/hold/670841/

Also, say I want to pick the block "map" from the parent template will not 
work without first walking manually up the chain, and again, build the 
context:

http://dpaste.com/hold/670842/

Alen






-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/sbqc07PVHbcJ.
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.



Re: Walking the template / Partial rendering

2011-12-09 Thread Tom Evans
On Fri, Dec 9, 2011 at 3:16 PM, Alen Mujezinovic
 wrote:
> Whoops. Editor ate the formatting. Here's the correctly formatted
> version: https://gist.github.com/d7b50c7449dfd09725ea
>

That version is much easier to read :)

This ticket renders email contents from different blocks within a template:

https://code.djangoproject.com/attachment/ticket/17193/send_templated_mail.3.diff

Their code for finding and rendering a block looks pretty simple, and
doesn't require changes to core to achieve it. Could it be applicable
to your needs? Check out _render_block_node()

Cheers

Tom

-- 
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.



Re: Walking the template / Partial rendering

2011-12-09 Thread Alen Mujezinovic
Whoops. Editor ate the formatting. Here's the correctly formatted 
version: https://gist.github.com/d7b50c7449dfd09725ea

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/n6YA8FHUauUJ.
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.



Walking the template / Partial rendering

2011-12-09 Thread Alen Mujezinovic
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:
        {% block body %}        {% block map %}{%
endblock %}        {% block content %}{% endblock %}            {%
endblock %}            {% extends "body.html" %}
{% block map %}{% endblock %}    {% block content %}
     {{ object_list|unordered_list }}    {% endblock %}            {% 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
        {% 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.