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.

On 26 Sep 2017 21:18, "Brice Parent" <[email protected]> wrote:

> 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.
>
> Le 26/09/17 à 14:34, Jonas H a écrit :
>
> 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. Example:
>
> class Money:
>     def __init__(self, amount, currency):
>         self.amount = amount
>         self.currency = currency
>
>     def __str__(self):
>         return '%s %s' % (self.currency, self.amount)
>
>     def __html__(self):
>         # Always show amount and currency on same line
>         return '%s\xa0%s' % (self.currency, self.amount)
>
> `conditional_escape` and friends already consider the __html__ method, and
> this works out well:
>
> >>> str(Money(1, '$'))
> '$ 1'
> >>> conditional_escape(Money(1, '$'))
> '$\xa01'
>
> In templates however it doesn't work that way because variables are always
> turned into strings before stuffing them into `conditional_escape` (see
> https://github.com/django/django/blob/98706bb35e7de0e445cc336f669919
> 047bf46b75/django/template/base.py#L977). My suggestion is to change the
> behaviour of that function so that it works as follows:
>
> - Given I write {{ foo }}
> - Does foo have a __html__ method? If yes, return `foo.__html__()`
> - Otherwise, return `conditional_escape(str(foo))`
>
> Do think that's a good idea?
>
> Jonas
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/django-developers/ea088de2-e538-4808-a7fd-8726929e2b91%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/django-developers/b3d37420-052d-74a2-5a39-e10d4c180f17%40brice.xyz?utm_medium=email&utm_source=footer>
> .
> 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 [email protected].
To post to this group, send email to [email protected].
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to