A template processor.
Install with pip:
pip install 'git+https://github.com/paiv/template-py'
import template
template.format(text, context=None, varchar='&', /, **kwargs)
The only substitution is of &
variables in the form:
&name
&{name}
Basic substitution:
template.format('&v &{v}', v='hello')
'hello hello'
If a value is not provided, the variable will not be substituted.
template.format('&v &s, &{v} &{s}', v='hello')
'hello &s, hello &{s}'
Environment can be provided in a dictionary:
context = dict(v='hello')
template.format('&v &{v}', context)
'hello hello'
Environment values can be of the types:
str
- simple stringgenerator
- a generator of stringscallable
- a function returningstr
or agenerator
- any other type will be converted to
str
The leading character of variables can be changed:
template.format('#v #{v}', None, '#', v='hello')
'hello hello'
import template
def gen():
yield 'alice\n'
yield 'bob\n'
context = dict()
context['data'] = gen
tpl = '''
Greetings,
&data
'''
template.format(tpl, context)
'''
Greetings,
alice
bob
'''