In the web2py book we see this code
def first():
form = SQLFORM.factory(Field('visitor_name', requires=IS_NOT_EMPTY()))
if form.process().accepted:
session.visitor_name = form.vars.visitor_name
redirect(URL('second'))
return dict(form=form)
being used in a template like so:
{{extend 'layout.html'}}
What is your name?
{{=form}}
And the question becomes: normally python values like strings and numbers are passed in the dictionary to the template engine. But what is being passed in this case and what is it about web2py that allows it to “unroll” it into HTML?
Can I build DOM trees and pass them? Or is this just a big escaped string?
In this case, the FORM object (class) is being passed. FORM is a gluon.html object, as are DIV, SPAN, P, etc. They all have a method called “.xml” which ‘unrolls’ the class into an HTML appropriate repsentation.
In web2py, you can pass any object in the dict() at the end of the controller, and use it in the template. Because web2py uses pure python in templates, you don’t have to make sure your object is “supported” in any way, any object or primitive can be sent. But, HTML objects should inherit from the base class of all HTML objects and should have an .xml() method which represents the object.
You can pass a
dom treeas you put it, assuming you mean a snippet of HTML (hierarchically structured in code). For example:Is totally valid and will ‘unfold’ properly [assuming I typed it correctly…]
For more, visit the epydocs: http://www.web2py.com/examples/static/epydoc/web2py.gluon.html.XmlComponent-class.html
There’s the “base HTML class” i mentioned — it’s actually called XMLComponent.
here’s the select class:
http://www.web2py.com/examples/static/epydoc/web2py.gluon.html.SELECT-class.html