Q:

Rendering jinja2 blocks as json

I've created a jinja2 template that should support both standalone rendering and ajax rendering.
The template looks like:
{% if is_ajax %}{% extends 'ajax.html' %}{% else %}{% extends 'site.html' %}{% endif %}
{% block title %}The Title{% endblock %}
{% block body %}The body{% endblock %}

site.html works fine, but I failed to create ajax.html that will render the title and body blocks as json. I tried something like:
{
    "copy":"{% block body %}Default Body{% endblock %}",
    "title":"{% block title %}Default Title{% endblock %}",
}

But this of course doesn't convert the blocks to json.
I've also tried to use Template.blocks to create the json using python, but this method requires manual rendering of each block, which doesn't feel like the right way of doing it.
Is this possible?

A:

Solved by using the self.block_name() directive. The dumps is actually 'json.dumps' passed into the template.
{
    "copy" : {{ dumps(self.copy()) | safe }},
    "title" : {{ dumps(self.title()) | safe }}
}

