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)
| 4609 | |
| 4610 | |
| 4611 | def template(*args, **kwargs): |
| 4612 | """ |
| 4613 | Get a rendered template as a string iterator. |
| 4614 | You can use a name, a filename or a template string as first parameter. |
| 4615 | Template rendering arguments can be passed as dictionaries |
| 4616 | or directly (as keyword arguments). |
| 4617 | """ |
| 4618 | tpl = args[0] if args else None |
| 4619 | for dictarg in args[1:]: |
| 4620 | kwargs.update(dictarg) |
| 4621 | adapter = kwargs.pop('template_adapter', SimpleTemplate) |
| 4622 | lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) |
| 4623 | tplid = (id(lookup), tpl) |
| 4624 | if tplid not in TEMPLATES or DEBUG: |
| 4625 | settings = kwargs.pop('template_settings', {}) |
| 4626 | if isinstance(tpl, adapter): |
| 4627 | TEMPLATES[tplid] = tpl |
| 4628 | if settings: TEMPLATES[tplid].prepare(**settings) |
| 4629 | elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: |
| 4630 | TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings) |
| 4631 | else: |
| 4632 | TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings) |
| 4633 | if not TEMPLATES[tplid]: |
| 4634 | abort(500, 'Template (%s) not found' % tpl) |
| 4635 | return TEMPLATES[tplid].render(kwargs) |
| 4636 | |
| 4637 | |
| 4638 | mako_template = functools.partial(template, template_adapter=MakoTemplate) |