Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor global template() #1322

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 22 additions & 18 deletions bottle.py
Expand Up @@ -4225,24 +4225,28 @@ 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)
Expand Down