Django

Code

Ticket #2181 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] templatetag should do openbrace, closebrace

Reported by: amcnabb@mcnabbs.org Assigned to: adrian
Milestone: Component: Template system
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

defaulttags.py (28.8 kB) - added by ubernostrum on 06/17/06 23:00:30.
one-half of a patch to implement this
__init__.py (31.4 kB) - added by ubernostrum on 06/17/06 23:00:49.
the other half
defaulttags.diff (1.2 kB) - added by ubernostrum on 06/17/06 23:05:07.
Add the right files this time
__init__.diff (403 bytes) - added by ubernostrum on 06/17/06 23:05:24.
Add the right files this time

Change History

06/17/06 22:04:32 changed by adrian

  • status changed from new to closed.
  • resolution set to invalid.

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

{% includegraphics filename %}

Example:

{% includegraphics 'somefile.jpg' %}

06/17/06 22:13:54 changed by amcnabb@mcnabbs.org

  • status changed from closed to reopened.
  • resolution deleted.

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.

06/17/06 22:16:34 changed by adrian

  • status changed from reopened to closed.
  • resolution set to wontfix.

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.

06/17/06 22:34:33 changed by amcnabb@mcnabbs.org

  • status changed from closed to reopened.
  • resolution deleted.

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.

06/17/06 22:49:57 changed 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!

06/17/06 22:53:42 changed by amcnabb@mcnabbs.org

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

06/17/06 23:00:30 changed by ubernostrum

  • attachment defaulttags.py added.

one-half of a patch to implement this

06/17/06 23:00:49 changed by ubernostrum

  • attachment __init__.py added.

the other half

06/17/06 23:01:58 changed 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.

06/17/06 23:04:24 changed 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)

06/17/06 23:05:07 changed by ubernostrum

  • attachment defaulttags.diff added.

Add the right files this time

06/17/06 23:05:24 changed by ubernostrum

  • attachment __init__.diff added.

Add the right files this time

06/17/06 23:13:02 changed by mtredinnick

  • status changed from reopened to closed.
  • resolution set to fixed.

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


Add/Change #2181 ([patch] templatetag should do openbrace, closebrace)




Change Properties
Action