Hi Thomas
As I understand there wouldn't be much required by Django to support this - just one function. So if it came to Python, I think we'd like to see a working snippet, maybe in a third party package, before merging support.
First, there's syntactic macros in PEP 638 (
https://www.python.org/dev/peps/pep-0638/ ). They would provide some extensibility to Python's grammar, so features like your HTML string would not need to go through PEP's and Python releases. They could just be installable packages. Definitely check that out - it seems like it's still in draft phase though.
Second, a related way to emulate such macros currently is with a codec. Check out the future-fstrings package, which backported f-strings to older versions of Python:
https://github.com/asottile/future-fstrings . As a "wrapper" text codec, it gets to pre-process the source code before passing it on to Python to execute. You could do your template string transformation with the current version of Python like this.
Third, stack inspection would also allow you to emulate template strings. Take this example:
import inspect
from django.utils.html import format_html
def html(string):
locals_ = inspect.currentframe().f_back.f_locals
return format_html(string, **locals_)
def main():
name = "Adam"
statement = "I'm < 120 years old"
print(html("<strong>{name}</strong> says <em>{statement}</em>"))
if __name__ == "__main__":
main()
Running it:
$ python ex.py
<strong>Adam</strong> says <em>I'm < 120 years old</em>