How about doing it this way:
class jsonrender:
def __getattr__(self, name):
return self
def __call__(self, d):
return simplejson.dumps(d)
mimerender = web.mimerender(
html=render.html,
xml=render.xml,
json=jsonrender()
)
class Hello:
def GET(self, name):
msg = "hello, " + (name or "world")
return render.hello({"message": msg})
Here multirender dispatches to appropriate render bases as HTTP_ACCEPT.
I tried this approach in one of my project and it turned out that I
don't need json for all pages. Probably supporting _accept parameter
to render argument may help.
For example, it probably makes sense to allow only html encoding for
user registrations.
class register:
def GET(self):
return render.register(_accept=["html"])
Another approach that I tried was this:
app.add_processor(accept_processor)
class hello:
def GET(self, name):
name = name or "world"
return render.hello(name)
def GET_json(self, name):
return {"message": "Hello, " + (name or "world")}
def GET_xml(self, name):
name = name or "world"
return render_xml.hello(name)
Here I assumed that accept_processor knows when to convert data to
json. But we can explicitly specify by adding multiple
accept_processors, each handling only one mimetype.
app.add_processor(accept_processor(prefix="xml", mimetype="text/xml"))
app.add_processor(accept_processor(prefix="json",
mimetype="application/json", process=lambda d: simplejson.dumps(d)))
This delegates all text/xml requests to GET_xml method and
application/json requests to GET_json method.
any comments?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---