def jinja_render(path, context, functions=(), ignore_undefined=False):
kwargs = {}
if ignore_undefined:
kwargs['undefined'] = SilentUndefined
else:
kwargs['undefined'] = jinja2.StrictUndefined
env = jinja2.Environment(loader=jinja2.FileSystemLoader(
os.path.dirname(path)), **kwargs)
env.filters['host'] = get_host
# FIXME: gethostbyname should be only used during config files render
env.filters['gethostbyname'] = lambda x: x
for func in functions:
env.globals[func.__name__] = func
env.globals['raise_exception'] = j2raise
content = env.get_template(os.path.basename(path)).render(context)
return content
评论列表
文章目录