diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-06-07 09:59:14 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-06-07 09:59:14 +0200 |
| commit | 4464bbba15d50ed32beb5995c13d26791ca61fe4 (patch) | |
| tree | 6b5e2800c14b62db83ad3c1f723074b2153a1c14 /django | |
| parent | 2c57809a560cb67c79b9e8a77cc713e8a2424c8e (diff) | |
Fixed #14502 -- Added a verbatim template tag.
Thanks SmileyChris for the patch.
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/base.py | 16 | ||||
| -rw-r--r-- | django/template/defaulttags.py | 35 |
2 files changed, 48 insertions, 3 deletions
diff --git a/django/template/base.py b/django/template/base.py index 7cb9807bab..9b2404cdb3 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -184,6 +184,7 @@ class Lexer(object): self.template_string = template_string self.origin = origin self.lineno = 1 + self.verbatim = False def tokenize(self): """ @@ -203,15 +204,26 @@ class Lexer(object): If in_tag is True, we are processing something that matched a tag, otherwise it should be treated as a literal string. """ - if in_tag: + if in_tag and token_string.startswith(BLOCK_TAG_START): # The [2:-2] ranges below strip off *_TAG_START and *_TAG_END. # We could do len(BLOCK_TAG_START) to be more "correct", but we've # hard-coded the 2s here for performance. And it's not like # the TAG_START values are going to change anytime, anyway. + block_content = token_string[2:-2].strip() + if self.verbatim and block_content == self.verbatim: + self.verbatim = False + if in_tag and not self.verbatim: if token_string.startswith(VARIABLE_TAG_START): token = Token(TOKEN_VAR, token_string[2:-2].strip()) elif token_string.startswith(BLOCK_TAG_START): - token = Token(TOKEN_BLOCK, token_string[2:-2].strip()) + if block_content.startswith('verbatim'): + bits = block_content.split(' ', 1) + if bits[0] == 'verbatim': + if len(bits) > 1: + self.verbatim = bits[1] + else: + self.verbatim = 'endverbatim' + token = Token(TOKEN_BLOCK, block_content) elif token_string.startswith(COMMENT_TAG_START): content = '' if token_string.find(TRANSLATOR_COMMENT_MARK): diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 1e2198fc67..3073b41049 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -6,7 +6,7 @@ from datetime import datetime from itertools import groupby, cycle as itertools_cycle from django.conf import settings -from django.template.base import (Node, NodeList, Template, Library, +from django.template.base import (Node, NodeList, Template, Context, Library, TemplateSyntaxError, VariableDoesNotExist, InvalidTemplateLibrary, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END, @@ -425,6 +425,13 @@ class URLNode(Node): else: return url +class VerbatimNode(Node): + def __init__(self, content): + self.content = content + + def render(self, context): + return self.content + class WidthRatioNode(Node): def __init__(self, val_expr, max_expr, max_width): self.val_expr = val_expr @@ -1273,6 +1280,32 @@ def url(parser, token): return URLNode(viewname, args, kwargs, asvar) @register.tag +def verbatim(parser, token): + """ + Stops the template engine from rendering the contents of this block tag. + + Usage:: + + {% verbatim %} + {% don't process this %} + {% endverbatim %} + + You can also specify an alternate closing tag:: + + {% verbatim -- %} + ... + {% -- %} + """ + bits = token.contents.split(' ', 1) + if len(bits) > 1: + closing_tag = bits[1] + else: + closing_tag = 'endverbatim' + nodelist = parser.parse((closing_tag,)) + parser.delete_first_token() + return VerbatimNode(nodelist.render(Context())) + +@register.tag def widthratio(parser, token): """ For creating bar charts and such, this tag calculates the ratio of a given |
