Mercurial > p > roundup > code
comparison roundup/cgi/engine_jinja2.py @ 6054:ffaf89a4a9d9
use jinja2 i18n extension
enable autoescape for jinja2 templates
| author | Christof Meerwald <cmeerw@cmeerw.org> |
|---|---|
| date | Mon, 13 Jan 2020 21:18:42 +0000 |
| parents | 238dafbe521c |
| children | ec1f725f5c91 |
comparison
equal
deleted
inserted
replaced
| 6053:380dec305c28 | 6054:ffaf89a4a9d9 |
|---|---|
| 45 | 45 |
| 46 class Jinja2Loader(LoaderBase): | 46 class Jinja2Loader(LoaderBase): |
| 47 def __init__(self, dir): | 47 def __init__(self, dir): |
| 48 self._env = jinja2.Environment( | 48 self._env = jinja2.Environment( |
| 49 loader=jinja2.FileSystemLoader(dir), | 49 loader=jinja2.FileSystemLoader(dir), |
| 50 extensions=[] | 50 extensions=['jinja2.ext.i18n'], |
| 51 autoescape=True | |
| 51 ) | 52 ) |
| 52 | 53 |
| 53 # Adding a custom filter that can transform roundup's vars to unicode | 54 # Adding a custom filter that can transform roundup's vars to unicode |
| 54 # This is necessary because jinja2 can only deal with unicode objects | 55 # This is necessary because jinja2 can only deal with unicode objects |
| 55 # and roundup uses utf-8 for the internal representation. | 56 # and roundup uses utf-8 for the internal representation. |
| 85 self._tpl = template | 86 self._tpl = template |
| 86 | 87 |
| 87 def render(self, client, classname, request, **options): | 88 def render(self, client, classname, request, **options): |
| 88 # [ ] limit the information passed to the minimal necessary set | 89 # [ ] limit the information passed to the minimal necessary set |
| 89 c = context(client, self, classname, request) | 90 c = context(client, self, classname, request) |
| 90 c.update({'options': options}) | 91 |
| 92 c.update({'options': options, | |
| 93 'gettext': lambda s: s2u(client.gettext(s)), | |
| 94 'ngettext': lambda s, p, n: s2u(client.ngettext(s, p, n))}) | |
| 91 s = self._tpl.render(c) | 95 s = self._tpl.render(c) |
| 92 return s if sys.version_info[0] > 2 else \ | 96 return s if sys.version_info[0] > 2 else \ |
| 93 s.encode(client.STORAGE_CHARSET, ) | 97 s.encode(client.STORAGE_CHARSET, ) |
| 94 | 98 |
| 95 def __getitem__(self, name): | 99 def __getitem__(self, name): |
