diff tests/test-template-engine.t @ 35468:32c278eb876f

templater: keep default resources per template engine (API) This allows us to register a repo object as a resource in hgweb template, without loosing '{repo}' symbol: symbol('repo') -> mapping['repo'] (n/a) -> defaults['repo'] resource('repo') -> mapping['repo'] (n/a) -> resources['repo'] I'm thinking of redesigning the templatekw API to take (context, mapping) in place of **(context._resources + mapping), but that will be a big change and not implemented yet. props['templ'] is ported to the resources dict as an example. .. api:: mapping does not contain all template resources. use context.resource() in template functions.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 21 Dec 2017 21:29:06 +0900
parents 511d6ae462f3
children 817a3d20dd01
line wrap: on
line diff
--- a/tests/test-template-engine.t	Thu Dec 21 21:03:25 2017 +0900
+++ b/tests/test-template-engine.t	Thu Dec 21 21:29:06 2017 +0900
@@ -4,8 +4,9 @@
   > from mercurial import templater
   > 
   > class mytemplater(object):
-  >     def __init__(self, loader, filters, defaults, aliases):
+  >     def __init__(self, loader, filters, defaults, resources, aliases):
   >         self.loader = loader
+  >         self._resources = resources
   > 
   >     def process(self, t, map):
   >         tmpl = self.loader(t)
@@ -13,7 +14,9 @@
   >             if k in ('templ', 'ctx', 'repo', 'revcache', 'cache', 'troubles'):
   >                 continue
   >             if hasattr(v, '__call__'):
-  >                 v = v(**map)
+  >                 props = self._resources.copy()
+  >                 props.update(map)
+  >                 v = v(**props)
   >             v = templater.stringify(v)
   >             tmpl = tmpl.replace('{{%s}}' % k, v)
   >         yield tmpl