def compile_template_with_filters(template_string, filters):
"""Compile a Django template, using additional filters.
This is like Template(template_string) except that additional
filters to be made available to the template may be specified.
Normally, one would define filters as documented in [1], but this
requires the INSTALLED_APPS settings to be set, which is not the
case in NAV[2]. This function is just a hack to get around that
limitation. The code is based on
django.template.compile_string[3].
filters should be a dictionary mapping filter names to functions.
[1]: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
[2]: https://nav.uninett.no/wiki/devel:django_introduction#settings
[3]: http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py
"""
lib = template.Library()
for name in filters.keys():
lib.filter(name, filters[name])
lexer = template.Lexer(template_string, None)
parser = template.Parser(lexer.tokenize())
parser.add_library(lib)
return parser.parse()
评论列表
文章目录