In case someone with the same question finds this.

I've subclassed Template to accept a list of template.Libraries:

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()


On Jul 17, 4:18 pm, A Khodyrev <a.v.khody...@gmail.com> wrote:
>   Hello
>
> I'm writing a library of helper classes/functions to ease creation of
> custom template tags. So a natural doctest (or an example in the
> documentation) goes like this: define a compiler function and Node
> class with my helpers, register them with a tag library, define a
> bunch of template strings with these tags, then render these templates
> and check whether the output is ok. However, there is a problem. For
> this to work, I need to somehow tell my templates where to find the
> tags. I can not use the {% load %} tag (because the tags being tested
> are not in a module), so I need to somehow tell the Template object
> where to look. I looked through django.template and there seems to be
> no way to do that (libraries are added to the template's Parser object
> which is deeply incapsulated).
>
> So, my question is: how do people test such things?
>
> --
> Alexander Khodyrev
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to