On 03/15/2015 07:10 AM, wilk wrote:
Of course you can also make a custom renderer:
from pyramid.compat import string_types
def html_renderer_factory(info):
def _render(value, system):
if not isinstance(value, string_types):
value = str(value)
return value
return _render
config.add_renderer('html', html_renderer_factory)
Then:
@view_config(renderer='html')
def myview(request):
return '<html><body>Hi</body></body>'
Don't you think an html renderer should go in the core of pyramid ?
It will be fine for one-file or any short pyramid app.
I think we should fix the string renderer instead TBH. I dont think
there should be one-renderer-per-content-type (e.g. I don't think it
would be sane to create a "CSS renderer"); instead I think we should
allow the string renderer to be used and the content-type to be
specified on a view-by-view case. It's currently just a bug that it
cannot be, and only in the case of 'text/html' (again look at the
string renderer implementation to see why).
With all the ways you can currently cause an HTML response to be
generated when you return a string, I don't think we need to add more
code to Pyramid itself.
Maybe even, returning a string without any renderer could use this since
it's already the default when we use Response (when we look at
trypyramid.com, html content type is not explicit).
There is already a way that this can currently be done on a global basis
without adding any code to Pyramid:
def html_response_adapter(s):
response = Response(s)
response.content_type = 'text/html'
return response
config.add_response_adapter(html_response_adapter, str)
Then you don't have to do anything special in any view_config, just
return a string and it will be treated as HTML, e.g.:
@view_config(route_name='/')
def aview(request):
return '<html><body>Hi</body></html>'
I don't think it's defensible for Pyramid to by default register this
response adapter, as it adds unnecessary documentation burden.
Arguments about defaults are always opinion-laden, sorry, but I'm apt to
rule against it because in general I think Pyramid tries to err on the
side of "maximally flexible" rather than "maximally convenient for the
new user" for better or worse.
We do however probably need to fix the string renderer such that you can
set 'text/html' as the content type in the view and not have it
overridden by the string renderer to 'text/plain'. This bug is actually
due to a shortcoming in WebOb, not in Pyramid, so will require a fix there.
- C
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.