Author scoder
Recipients
Date 2005-03-07.11:50:56
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
A common use case for templates is partially replacing
placeholders in loops or method cascades. The
safe_substitute method of the Template class provides
rudimentary support for this as long as Templates do
not contain the "$$" escape. In cases where multiple
replacements are necessary, a new Template must be
created. This messes up the "$$" replacement.

An example:

.>>> from string import Template
.>>> a = Template('$a + $$ != $b')
.>>> b = Template(a.safe_substitute(a=1))
.>>> [ b.substitute(b=i) for i in range(3) ]
Traceback [...]
ValueError: Invalid placeholder in string: line 1, col 5

In the current implementation, there is no way of
getting around this problem.

I suggest adding a new class-method as constructor to
the Template class: "Template.from_template", that
takes a template, applies a safe_substitute with the
provided (kw)arguments and returns a *valid* Template
(not a string). However, this method *must not* replace
occurrences of "$$" in the original template.

The above example would then look like this:
.>>> from string import Template
.>>> a = Template('$va + $$ != $vb')
.>>> b = Template.from_template(a, va=1)
.>>> [ b.substitute(vb=i) for i in range(3) ]
[ '1 + $ != 0', '1 + $ != 1', '1 + $ != 2' ]
History
Date User Action Args
2007-08-23 16:10:34adminlinkissue1158231 messages
2007-08-23 16:10:34admincreate