Proposal: Support the __html__ method as an alternative/addition to the __str__ for turning objects into strings in the template layer.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/16f056c0-0ed8-424f-ae9e-9ab7a7bf66cb%40googlegroups.com.
I like the idea a lot. But wouldn't it be better to have it as a
separate library? It seems to be something that could be quite
common, and have many use cases both inside and outside Django (we
could sometimes benefit from having an html exception message for
example).
And I guess that would mean we should also have an html() function which would call .__html__() if it exists, and fallback to .__str__() if not.
Django's template would call this html() function in its templates, while models and other objects could just declare this __html__() method.
And if it gets adopted by more than just Django, it could be integrated into the standard Python library and benefit to many more projects.
Only question: Where should it stop? Should there also be a
__json__, a __yaml__ and an __xml__ methods? Those are also quite
common representations we could want from a class, even more
nowadays that a big tendency is to develop microservices which
communicate through APIs, and frontends being more and more
delegated to javascript libraries.
--
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-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ea088de2-e538-4808-a7fd-8726929e2b91%40googlegroups.com.
Collin Anderson:
I think this is a good idea. Django has used __html__ internally for the last 4 years so it's not something new [0]. I'm surprised this doesn't work out-of-the box.
Brice Parent:
I like the idea a lot. But wouldn't it be better to have it as a separate library? It seems to be something that could be quite common, and have many use cases both inside and outside Django (we could sometimes benefit from having an html exception message for example).
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ea088de2-e538-4808-a7fd-8726929e2b91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/b3d37420-052d-74a2-5a39-e10d4c180f17%40brice.xyz.
The problem with something more complex like yaml or json is it's not easy to combine the output. If those methods return a string, as in actual json, it's not easy to do anything with them (like combine them into an array or another object). YAML is also whitespace sensitive. If they return a dict or some python object that can be combined and serialised as a whole, then that's kind of confusing and could be more generic.
XML and HTML don't suffer as much from this I think.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAFNZOJP45PchAC_1kqMOek0TRwfTc0qavfZ2Qf_-TzTpZto_eA%40mail.gmail.com.
--
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-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
Only question: Where should it stop? Should there also be a __json__, a __yaml__ and an __xml__ methods?
Aymeric Augustin:
This could be a regression
On 26 Sep 2017, at 22:56, Jonas Haag <jo...@lophus.org> wrote:
Without having had a look at any of the links you’ve posted, this does NOT work in Jinja2 the way I suggested:>>> jinja2.Template('{{foo}}').render({'foo': Money('1', '$')})'$ 1'So whatever the changes for better __html__ interoperability were, they seem unrelated to what I’m suggesting.
Aymeric Augustin:
On 26 Sep 2017, at 22:56, Jonas Haag <jo...@lophus.org> wrote:Without having had a look at any of the links you’ve posted, this does NOT work in Jinja2 the way I suggested:>>> jinja2.Template('{{foo}}').render({'foo': Money('1', '$')})'$ 1'So whatever the changes for better __html__ interoperability were, they seem unrelated to what I’m suggesting.That's because Jinja2 priorizes speed over security and disables autoescaping by default. To trigger __html__, you need to enable it.
Am 26.09.2017 um 22:50 schrieb Aymeric Augustin <aymeric....@polytechnique.org>:Hello,This could be a regression because https://github.com/django/django/commit/3483682749577b4b5a8141a766489d5b460e30e9 looks like it implemented that behavior;
Proposal: Support the __html__ method as an alternative/addition to the __str__ for turning objects into strings in the template layer.If this has been discussed before, please point me to it; I couldn't find anything with the search function.Some custom classes may have, in addition to a __str__ representation, a natural representation that is better suited for HTML output.