Allow parser to parse until "complex" block node

5 views
Skip to first unread message

Julien Phalip

unread,
Aug 13, 2008, 9:39:03 AM8/13/08
to Django developers
Hi,

Please let me know if this is worth considering for 1.0 or if it can
wait till later. Any feedback would be welcome.

Currently, with block tags, you can only parse until a "simple"
closure tag. For ex: {% endif %} or {% endfor %}.

Now, it would be nice if you could parse until something more
"complex" like {% elseif blah and blahblah %}. By "complex" I mean a
tag that requires some parameters.

Here I'm not asking for an "elseif" tag in particular, but I'd like to
allow the parser to also consider "complex" closure tags.

For example, a direct application of this would be a suggestion for a
{% withblock %} tag. Here's a suggested syntax:

{% withblock as myurl %}
{% url path.to.some_view arg1,arg2,name1=value1 %}
{% and as var %}
{% whatever %}
{% in %}
Click the link: <a href="{{ myurl }}">{{ var }}</a>.
{% endwithblock %}

Currently the {% and as var %} tag cannot be recognized. The problem
is in django.template.Parser.parse():

...
elif token.token_type == TOKEN_BLOCK:
if token.contents in parse_until:
...

It basically checks if the tag's hard string name is in the list. If
instead it did a regular expression match, then in your tag you could
parse the block like follows:

nodelist = parser.parse(('and .*', 'in')) # Parse until {% and as
var %} or {% in %}

I hope it makes sense. Please also let me know if this suggestion
breaks an earlier commitment made by the developers of the original
template syntax.

Thanks a lot!

Julien ;)

Malcolm Tredinnick

unread,
Aug 13, 2008, 10:16:38 AM8/13/08
to django-d...@googlegroups.com

On Wed, 2008-08-13 at 06:39 -0700, Julien Phalip wrote:
> Hi,
>
> Please let me know if this is worth considering for 1.0 or if it can
> wait till later.

Definitely post-1.0. It's a feature request that we've managed quite
easily to do without until now.

Regards,
Malcolm


Daniel Pope

unread,
Aug 13, 2008, 1:38:01 PM8/13/08
to django-d...@googlegroups.com
2008/8/13 Julien Phalip <jph...@gmail.com>:

> Currently the {% and as var %} tag cannot be recognized. The problem
> is in django.template.Parser.parse():

You could parse up to {% endwithblock %} and then look for {% and %}
Nodes in the nodelist you receive.

There are other ways of doing what you want if you allow different
syntax. You could just nest {% withblock %}{% in %}{% endwithblock %}
triples. Or, I achieved something similar to what you are doing with
syntax like

{% extends "base.html" %}
{% call "another_template.html" %}
{% block foo %}This is substituted into the foo block in the
template 'another_template.html' and not the foo block in 'base.html'
{% endblock %}
{% endcall %}

by writing a very thin wrapper around ExtendsNode.

Dan

Julien Phalip

unread,
Aug 13, 2008, 6:39:12 PM8/13/08
to Django developers
Thanks, both, for the hints. I agree this can wait till after 1.0, and
the proposed patch is backward compatible anyway.
Just for the record, I've submitted 2 tickets regarding this issue:

http://code.djangoproject.com/ticket/8296
http://code.djangoproject.com/ticket/8297

Regards,

Julien

On Aug 14, 3:38 am, "Daniel Pope" <lord.ma...@gmail.com> wrote:
> 2008/8/13 Julien Phalip <jpha...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages