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)
| 3605 | |
| 3606 | |
| 3607 | def template(*args, **kwargs): |
| 3608 | ''' |
| 3609 | Get a rendered template as a string iterator. |
| 3610 | You can use a name, a filename or a template string as first parameter. |
| 3611 | Template rendering arguments can be passed as dictionaries |
| 3612 | or directly (as keyword arguments). |
| 3613 | ''' |
| 3614 | tpl = args[0] if args else None |
| 3615 | adapter = kwargs.pop('template_adapter', SimpleTemplate) |
| 3616 | lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) |
| 3617 | tplid = (id(lookup), tpl) |
| 3618 | if tplid not in TEMPLATES or DEBUG: |
| 3619 | settings = kwargs.pop('template_settings', {}) |
| 3620 | if isinstance(tpl, adapter): |
| 3621 | TEMPLATES[tplid] = tpl |
| 3622 | if settings: TEMPLATES[tplid].prepare(**settings) |
| 3623 | elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl: |
| 3624 | TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings) |
| 3625 | else: |
| 3626 | TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings) |
| 3627 | if not TEMPLATES[tplid]: |
| 3628 | abort(500, 'Template (%s) not found' % tpl) |
| 3629 | for dictarg in args[1:]: kwargs.update(dictarg) |
| 3630 | return TEMPLATES[tplid].render(kwargs) |
| 3631 | |
| 3632 | mako_template = functools.partial(template, template_adapter=MakoTemplate) |
| 3633 | cheetah_template = functools.partial(template, template_adapter=CheetahTemplate) |