2

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.

4
  • Why wouldn't you directly instanciate t as Template("<b>${noun}</b>ification")? Commented Dec 12, 2017 at 16:00
  • @vmonteco If nouns is a list, I can write Template("${nouns.as_ul}"); then the output is a big unordered list. Imagine ${nouns.as_ul} inside an HTML template, I don't need some weird {% for n in nouns %}<li>{{ n }}</li>{% endfor %} but the method as_ul take care of it, nice and clean. Commented Dec 12, 2017 at 16:14
  • And what are you trying to achieve with all this? A web application? Commented Dec 12, 2017 at 16:19
  • @vmonteco For now just curiosity/studying but eventually a template engine. Even if they aren't so essential nowadays. Commented Dec 12, 2017 at 16:24

3 Answers 3

2

Not a direct answer, but I'd recommend not using Template strings.

If you're using Python 3.6 or newer, use f-strings:

def bold(t):
    return f"<b>{t}</b>"

noun = 'Egg'

print(f"{bold(noun)}ification")

when run:

<b>Egg</b>ification

If you'd rather a more powerful template mechanism, I highly recommend Jinja2.

Sign up to request clarification or add additional context in comments.

Comments

0

Wanted to do this myself, figured it out so came to update my google hit.

from string import Template
class MyTemplate(Template):
    idpattern = r'(?a:[_a-z][_.a-z0-9]*)'
    
s = MyTemplate("$myvar $my.var and ${myvar} and ${my.var}")
s.safe_substitute({'myvar': 'aaaa', 'my.var': 'bbbb'})

Result:

'aaaa bbbb and aaaa and bbbb'

Other over-ride options are both the delimiter ($) or braceidpattern (None) or pattern:

pattern = fr"""
{delim}(?:
  (?P<escaped>{delim})  |   # Escape sequence of two delimiters
  (?P<named>{id})       |   # delimiter and a Python identifier
  {{(?P<braced>{bid})}} |   # delimiter and a braced identifier
  (?P<invalid>)             # Other ill-formed delimiter exprs
)
"""

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.