pythontemplate-strings

Extending the Python Template strings


Is it possible to extent the Template strings so I can add methods to the identifier:

By default I can replace simple variables:

from string import Template
t = Template("${noun}ification")
t.substitute(dict(noun='Egg'))  # Output: 'Eggification'

But I wish to extend the identifier with a custom method (e.g. as_b):

t = Template("${noun.as_b}ification")  # Desired output: '<b>Egg</b>ification'

I know I can easily do something similar with Formatted string literals but is it possible using the Template strings, like above? I cannot use Formatted string literals because it will not work inside a script, a JSON file or whatever text that contain curly braces.

Essentially I'm looking for something that can offer the Template strings syntax within the Formatted string literals flexibility.


Solution

  • Genshi does exactly that but it doesn't seem to use the built-in string module.
    Other useful references are Chameleon and Mako.