Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#2181 closed enhancement (fixed)

[patch] templatetag should do openbrace, closebrace

Reported by: amcnabb@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The templatetag tag system isn't quite enough in some situations. For example, there are problems when templating a LaTeX file. There is a LaTeX command of the form \includegraphics{myfilename.jpg}. Unfortunately,
\includegraphics{{{ filename }}} is a syntax error in the templating system, so it's difficult to specify a filename to be filled in, especially since \includegraphics{ myfilename.jpg } is illegal in LaTeX (extra spaces aren't allowed). I would recommend adding {% templatetag openbrace %} and {% templatetag closebrace %} to the already existing templatetag to allow escaping in this situation.

Attachments (4)

defaulttags.py (28.8 KB ) - added by James Bennett 18 years ago.
one-half of a patch to implement this
__init__.py (31.4 KB ) - added by James Bennett 18 years ago.
the other half
defaulttags.diff (1.2 KB ) - added by James Bennett 18 years ago.
Add the right files this time
__init__.diff (403 bytes ) - added by James Bennett 18 years ago.
Add the right files this time

Download all attachments as: .zip

Change History (13)

comment:1 by Adrian Holovaty, 18 years ago

Resolution: invalid
Status: newclosed

To accomplish your example, just write a template tag with this syntax:

{% includegraphics filename %}

Example:

{% includegraphics 'somefile.jpg' %}

comment:2 by amcnabb@…, 18 years ago

Resolution: invalid
Status: closedreopened

That's a way to do it, but from my perspective as a user, it's an awkward solution. If I expected to do a lot of includegraphics commands, making a template tag would be a natural way to go. However, in an isolated case like this, where the real issue is escaping special characters, it would feel much more natural use templatetag.

I guess it might also be helpful if the template parser only looked at the innermost two braces in a string of braces, i.e. in the string {{{{{ the first three braces would be assumed to be part of the text. Right now the first two braces are considered to be the escape string, and the innermost three braces are considered part of the variable name, which is illegal syntax anyway.

Anyway, those are my thoughts.

comment:3 by Adrian Holovaty, 18 years ago

Resolution: wontfix
Status: reopenedclosed

Sorry, embedding variable template tags within block template tags is too ugly and unintuitive. Just write a custom template tag like I suggested -- it's easy and fun.

comment:4 by amcnabb@…, 18 years ago

Resolution: wontfix
Status: closedreopened

Whoa. I think I completely failed to communicate what the issue is.

All I want to do is put a curly brace next to a variable template tag! It takes me a half a screen of code to write a custom template tag, and it's all just to print one stupid little curly brace!

I've been really impressed with everything else in Django. I really like it a lot. But I can't justify writing half a screen of code, just to get one little curly brace.

I'm not trying to embed variable template tags within block template tags. All I want to do is something like this:

{% templatetag openbrace %}{{ somevariable }}

because I can't find any other way to put a brace in front of a variable template tag.

Sorry for my overbearing persistence, but this just seems way to complicated for what should be a really simple thing. If I'm doing a string in python, and I want to put in a quotation mark somewhere, all I have to do is put a backslash in front of my quotation mark, and everything's happy. I don't have to rewrite the parser or anything.

comment:5 by Adrian Holovaty, 18 years ago

OHHHHH, *now* I see what you want to do! Yeah, I completely misunderstood -- my apologies. That sounds like a good addition to the default template tag library. Whew!

comment:6 by amcnabb@…, 18 years ago

All in good fun. Thanks, and sorry for being unclear.

by James Bennett, 18 years ago

Attachment: defaulttags.py added

one-half of a patch to implement this

by James Bennett, 18 years ago

Attachment: __init__.py added

the other half

comment:7 by James Bennett, 18 years ago

I've attached patches which implement two extra constants for the template system to add a "single" variation of the variable start/end tags, and allows the templatetag tag to output them.

comment:8 by James Bennett, 18 years ago

Summary: templatetag should do openbrace, closebrace[patch] templatetag should do openbrace, closebrace

(also, as to whether this should go in, I'm pretty much neutral; if the template system does indeed choke on a single brace before a variable, then this is necessary, but I've not verified that)

by James Bennett, 18 years ago

Attachment: defaulttags.diff added

Add the right files this time

by James Bennett, 18 years ago

Attachment: __init__.diff added

Add the right files this time

comment:9 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: reopenedclosed

(In [3138]) Fixed #2181 -- allow '{' and '}' to be escaped via {% templatetag ... %}.

Note: See TracTickets for help on using tickets.
Back to Top