-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjinja_customs.py
36 lines (31 loc) · 1.1 KB
/
jinja_customs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# =============================================================================
# File: jinja_customs.py
# Author: Andre Brener
# Created: 13 Jun 2017
# Last Modified: 13 Jun 2017
# Description: description
# =============================================================================
import functools
import jinja2
def update_environment(env, filters):
filters_ = {}
for k, filter_ in filters.items():
filters_[k] = functools.partial(filter_, env)
env.filters.update(filters_)
return env
def load_templates(templates_folder='templates',
extensions=['html'],
trim_blocks=True,
filters=None):
loader = jinja2.FileSystemLoader(templates_folder)
env = jinja2.Environment(loader=loader)
if trim_blocks:
env.trim_blocks = True
env.lstrip_blocks = True
if filters:
env = update_environment(env, filters)
templates = [
t for t in loader.list_templates()
if any(t.endswith(ext) for ext in extensions)
]
return {t: env.get_template(t) for t in templates}