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.

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.

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&#x27;m &lt; 120 years old</em>


On Fri, 11 Jun 2021 at 08:52, guettli <guettli.goo...@thomas-guettler.de>
wrote:

> Hi,
>
> Since I use the html-fragments-over-the-wire pattern (htmx) I create
> small function based views returning small html fragments.
>
> Up to now I use format_html().
>
> I am tired of passing in the variables like `foo=foo, bar=bar` into
> format_html().
>
> I created a Python PEP to introduce a new way.
>
> The goal is to have something like the convenient f-string syntax
> combined with conditional_escape().
>
> The draft is here:
>
> Template Literals:
> https://github.com/guettli/peps/blob/master/pep-9999.rst
>
> I am looking for feedback. Either here or on python-ideas.
>
> If Python would implement this PEP, would you accept patches
> to Django to support Template Literals?
>
> Background: I don't like the JS-Frontend madness. Django can
> do more than just provide an http API.
>
> 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/39137657-4b16-42ad-a2ea-4d97ba9382d4n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/39137657-4b16-42ad-a2ea-4d97ba9382d4n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAMyDDM1uYWyJpSPZtO2B87kpE5Yt1X2wyWGMZ9HoobhvEtjaeg%40mail.gmail.com.
  • Pyt... guettli
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)

Reply via email to