From 73aaa69c81720f2a6b2ff16024ef71e58afa3456 Mon Sep 17 00:00:00 2001 From: SergBobrovsky Date: Mon, 11 Jan 2021 13:41:01 +0300 Subject: [PATCH 1/2] Update bottle.py --- bottle.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/bottle.py b/bottle.py index 23fff762..933fdcaf 100755 --- a/bottle.py +++ b/bottle.py @@ -4225,24 +4225,29 @@ def template(*args, **kwargs): Template rendering arguments can be passed as dictionaries or directly (as keyword arguments). """ - tpl = args[0] if args else None - for dictarg in args[1:]: - kwargs.update(dictarg) - adapter = kwargs.pop('template_adapter', SimpleTemplate) - lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) - tplid = (id(lookup), tpl) - if tplid not in TEMPLATES or DEBUG: - settings = kwargs.pop('template_settings', {}) - if isinstance(tpl, adapter): - TEMPLATES[tplid] = tpl - if settings: TEMPLATES[tplid].prepare(**settings) - elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: - TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings) - else: - TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings) - if not TEMPLATES[tplid]: - abort(500, 'Template (%s) not found' % tpl) - return TEMPLATES[tplid].render(kwargs) + tpl = name = source = res = args[0] if args else None + for dictarg in args[1:]: kwargs.update(dictarg) + settings, keys = kwargs.pop('template_settings', {}), [] + while isinstance(res, str): + lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) + adapter = kwargs.pop('template_adapter', SimpleTemplate) + if not ('\n' in name or '{' in name or '%' in name or '$' in name): + keys.append('\n'.join([name] + lookup)) # 1st cache key + res = TEMPLATES.get(keys[-1]) + if res: break + name = adapter.search(name=name, lookup=lookup) + keys.append(name) # 2st cache key, abspath + with open(name) as f: + source = f.read() + res = TEMPLATES.get(source) + if not res: + res = adapter(source=source, lookup=lookup, **settings) + keys.append(source) # 3th key, template content + if not isinstance(res, BaseTemplate): abort(500, "can't make temple from (%r)" % tpl) + if res is tpl and settings: res.prepare(**settings) + for key in keys: + TEMPLATES[key] = res + return res.render(kwargs) mako_template = functools.partial(template, template_adapter=MakoTemplate) From 961b96f8ccc530edfb9e2e6cd2d18d6eb8434786 Mon Sep 17 00:00:00 2001 From: SergBobrovsky Date: Mon, 11 Jan 2021 14:15:40 +0300 Subject: [PATCH 2/2] Update bottle.py --- bottle.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bottle.py b/bottle.py index 933fdcaf..a515a692 100755 --- a/bottle.py +++ b/bottle.py @@ -4245,8 +4245,7 @@ def template(*args, **kwargs): keys.append(source) # 3th key, template content if not isinstance(res, BaseTemplate): abort(500, "can't make temple from (%r)" % tpl) if res is tpl and settings: res.prepare(**settings) - for key in keys: - TEMPLATES[key] = res + for key in keys: TEMPLATES[key] = res return res.render(kwargs)