Template system improvement: "cat" filter, "include" tag

Skip to first unread message

David Avsajanishvili

Aug 2, 2008, 8:37:22 AM8/2/08
to django-d...@googlegroups.com

I have two suggestions how to improve Django template system (see
http://code.djangoproject.com/ticket/8088 ):

1. Since we have add filter, that adds a value to integer variable,
why not implement cat filter, which will concatenate strings? I think,
it would be useful for many developers!

2. include tag has two options: template name could be specified
either as a quoted string, or as a variable. But for all that template
filters cannot be used. It would be nice to allow developers to
specify template name more flexibly, for example:

{% include var_name|lower %}

or even like this (page5_ka.txt):

{% include "page"|cat:pageid|cat:LANGUAGE_CODE|cat:"_.txt" %}

The diffs attached to the ticket do these things. At the same time,
new revision of template system will be fully backwards-compatible
with previous. An only exception is - using quoted strings with quotes
inside, like this:

{% include "a"mysterious"template.txt" %}

Currently, template system accepts this string as a template name with
doublequotes inside, while modified - won't. But, IMHO, nobody will
name templates this case.


I have one template in my project to render some static pages,
which are also in templates directory, formatted with textile,
and included into my template with this tag:

{% block content %}

{% filter textile %}
{% include pagename|cat:"_"|cat:LANGUAGE_CODE|cat:".txt" %}
{% endfilter %}

{% endblock %}

Also, I have one URL entry in my urls.py:
direct_to_template, {'template': 'page.html'}),

0 code lines in models!

Of course, Thanks to flexible architecture of Django,
this behaviour could be easily implemented as custom template tags and filters
(as I did for my project), but maybe it would be useful for other
developers as well.

David Avsajanishvili

Aug 2, 2008, 1:36:55 PM8/2/08
to Django developers
A little correction to Example:

{% include params.pagename|cat:"_"|cat:LANGUAGE_CODE|cat:".txt" %}

("rules" --> "rules_ka.txt")

Tested. Works.

Of course, too many logic in template, but there are
cases, when views/models not used at all. It adds
extra flexibility to template system without violating
Reply all
Reply to author
0 new messages