Get a rendered template as a string iterator. You can use a name, a filename or a template string as first parameter. Template rendering arguments can be passed as dictionaries or directly (as keyword arguments).
(*args, **kwargs)
| 4211 | |
| 4212 | |
| 4213 | def template(*args, **kwargs): |
| 4214 | """ |
| 4215 | Get a rendered template as a string iterator. |
| 4216 | You can use a name, a filename or a template string as first parameter. |
| 4217 | Template rendering arguments can be passed as dictionaries |
| 4218 | or directly (as keyword arguments). |
| 4219 | """ |
| 4220 | tpl = args[0] if args else None |
| 4221 | for dictarg in args[1:]: |
| 4222 | kwargs.update(dictarg) |
| 4223 | adapter = kwargs.pop('template_adapter', SimpleTemplate) |
| 4224 | lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) |
| 4225 | tplid = (id(lookup), tpl) |
| 4226 | if tplid not in TEMPLATES or DEBUG: |
| 4227 | settings = kwargs.pop('template_settings', {}) |
| 4228 | if isinstance(tpl, adapter): |
| 4229 | TEMPLATES[tplid] = tpl |
| 4230 | if settings: TEMPLATES[tplid].prepare(**settings) |
| 4231 | elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: |
| 4232 | TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings) |
| 4233 | else: |
| 4234 | TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings) |
| 4235 | if not TEMPLATES[tplid]: |
| 4236 | abort(500, 'Template (%s) not found' % tpl) |
| 4237 | return TEMPLATES[tplid].render(kwargs) |
| 4238 | |
| 4239 | |
| 4240 | mako_template = functools.partial(template, template_adapter=MakoTemplate) |