Adam Johnson schrieb am Freitag, 11. Juni 2021 um 10:17:02 UTC+2:
> 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. > > Thank you Adam. The change in Django would be tiny. I think only HttpResponse and conditional_escape would need a change. I will provide the PR if the PEP gets accepted. > In terms of the PEP, - there are already "too many" ways to template a > string in Python, so I'm not sure it will be very popular. A few > alternatives spring to mind, beyond those discussed in your related forum > thread ( > https://forum.djangoproject.com/t/rethinking-how-to-create-html-f-string-like-with-conditional-escape/8212 > > ). > > 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. > > I will have a look at PEP 638. > 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. > > I will have a loot at this. I would be happy if no PEP would be needed. But one thing is important for me: IDE and linters should understand it. > 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() > > Because of the lack of IDE/linters support I added this to the "Rejected Ideas". Thank you for your feedback, I will look into your hints. Regards, Thomas -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/10b8b00e-e213-48e2-a9a7-7aa8479aefc5n%40googlegroups.com.