Is it possible to parse a django template and only render a specific tag ?
This snippet is close to what I’m looking for but it doesn’t return the entire template.
Basically if I have this template as input
<html>
<title>{% block title%}{% endblock %}</title>
<body>
{% block content %}
{% mycustomtag "args" %}
{% endblock content %}
</body>
</html>
and i want to only render mycustomtag this is the output Im looking for
<html>
<title>{% block title%}{% endblock %}</title>
<body>
{% block content %}
<p>Result from mycustomtag</p>
{% endblock content %}
</body>
</html>
Thanks.
If I properly understand your question, then there is a way to do that, using the
{% verbatim %}tag. It will be added in Django 1.5, but for now you can use it as your custom tag – here is the source: https://code.djangoproject.com/ticket/16318The only drawback here is that you cannot use directly this template, it needs double rendering. If this is what you need – then everything is OK.
To use it, all you need to do is to enclose the other tags with {% verbatim %} :
I’ve made a simple test with this template:
This prints the following:
Hope this could be helpful.