8.2 Template
The String module contains a widely used class, which provides simple syntax support for end-user editing. This allows users to customize their applications without modifying the applications.
This format uses a placeholder name in the form of $ plus a valid python identifier (numbers, letters, and underscores. You can use braces on both sides of the placeholder to separate the strings with more letters and numbers. Use $ to create a single $ transcoding character.
>>> From string import Template
>>> T = Template ('$ {village} folk send $10 to $ cause .')
>>> T. substitute (village = 'notthen', cause = 'the ditch fund ')
'Nottinghamfolk send $10 to the ditch fund .'
If the placeholder is not provided in the dictionary or keyword parameter, the substitute () method throws a key error. User data may be incomplete in applications separated by mail merge. This method may be more suitable if safe_substitude () is used-if data is lost, it will not change the placeholder.
>>> T = Template ('Return the $ item to $ owner .')
>>> D = dict (item = 'unladen swallow ')
>>> T. substitute (d)
Traceback (most recent call last ):
...
KeyError: 'owner'
>>> T. safe_substitute (d)
'Return the unladen swallow to $ owner .'
A custom separator can be specified for a template derived class. For example, you can use the percent sign as a placeholder for the batch naming tool class of the image browser, such as the current date, image serial number, or file format.
Html webpage custom templates are called possible.