Template class to test custom tag libraries

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.template import Template, TemplateEncodingError,
StringOrigin, Lexer, Parser
from django.utils.encoding import smart_unicode

class TestableTemplate(Template):
    def __init__(self, template_string, origin=None,
            name='<Unknown Template>', libraries=[]):
        try:
            template_string = smart_unicode(template_string)
        except UnicodeDecodeError:
            raise TemplateEncodingError("Templates can only be
constructed from unicode or UTF-8 strings.")
        origin = StringOrigin(template_string)
        self.nodelist = my_compile_string(template_string, origin,
libraries)
        self.name = name

def my_compile_string(template_string, origin, libraries=[]):
    "Compiles template_string into NodeList ready for rendering"
    lexer = Lexer(template_string, origin)
    parser = Parser(lexer.tokenize())
    for lib in libraries:
        parser.add_library(lib)
    return parser.parse() 

More like this

  1. Safe template decorator by eternicode 1 year ago
  2. Mod to allow simple_tag to access context by leaf 4 years, 7 months ago
  3. testdata tag for templates by showell 4 years ago
  4. Parse TemplateTag Variables Safely by evan_schulz 4 years, 10 months ago
  5. SASS/SCSS include template tag. by bryanhelmig 1 year, 3 months ago

Comments

(Forgotten your password?)