Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#2181 closed enhancement (fixed)

[patch] templatetag should do openbrace, closebrace

Reported by: amcnabb@… Owned by: adrian
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: UI/UX:

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 ubernostrum 8 years ago.
one-half of a patch to implement this
__init__.py (31.4 KB) - added by ubernostrum 8 years ago.
the other half
defaulttags.diff (1.2 KB) - added by ubernostrum 8 years ago.
Add the right files this time
__init__.diff (403 bytes) - added by ubernostrum 8 years ago.
Add the right files this time

Download all attachments as: .zip

Change History (13)

comment:1 Changed 8 years ago by adrian

  • Resolution set to invalid
  • Status changed from new to closed

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

{% includegraphics filename %}

Example:

{% includegraphics 'somefile.jpg' %}

comment:2 Changed 8 years ago by amcnabb@…

  • Resolution invalid deleted
  • Status changed from closed to reopened

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 Changed 8 years ago by adrian

  • Resolution set to wontfix
  • Status changed from reopened to closed

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 Changed 8 years ago by amcnabb@…

  • Resolution wontfix deleted
  • Status changed from closed to reopened

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 Changed 8 years ago by adrian

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 Changed 8 years ago by amcnabb@…

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

Changed 8 years ago by ubernostrum

one-half of a patch to implement this

Changed 8 years ago by ubernostrum

the other half

comment:7 Changed 8 years ago by ubernostrum

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 Changed 8 years ago by ubernostrum

  • Summary changed from templatetag should do openbrace, closebrace to [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)

Changed 8 years ago by ubernostrum

Add the right files this time

Changed 8 years ago by ubernostrum

Add the right files this time

comment:9 Changed 8 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from reopened to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.