Question

i used rows.xml() to generate html output. i want to know how to add html codes to this generated html page e.g: "add logo, link css file,.. etc"

rows=db(db.member.membership_id==request.args[0]).select(db.member.membership_id ,db.member.first_name,db.member.middle_name ,db.member.last_name) return rows.xml()

Was it helpful?

Solution

There are many HTML helpers you can use, for example:

html_code = A('<click>', rows.xml(), _href='http://mylink')
html_code = B('Results:', rows.xml(), _class='results', _id=1)
html_page = HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1)))

and so on.

You can even create a whole table automatically:

table = SQLTABLE(rows, orderby=True, _width="100%")

and then pick it apart to insert links or modify its elements.

It is very powerful and normally you don't have to bother writing the actual HTML yourself. Here is the cheatsheet, or you can check directly on the website documentation.


Edit: Just to make sure, you don't actually need to generate the whole HTML page, it is easier to let web2py insert your response in a template that has the same name as your controller (or force a particular template with response.view = 'template.html'. The documentation tutorial will explain that better and in further details.

In a few words, if you are implementing the function index, you could either return a string (the whole page HTML, which seems to be what you are heading for), or a dictionary to use templates.

In the first case, just code your function like this:

def index():
    # ... code to extract the rows
    return HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1))).xml()

Otherwise, write an html template in views/controller/index.html (or another file if you insert the response.view=... in your function, to re-use the same template), which could be like this:

<html><head></head>
  <body>
    {{=message}}
  </body>
</html>

and return a dictionary:

def index():
    # ... code to extract the rows
    html = B('Results:', rows.xml(), _class='results', _id=1)
    return dict(message=html)

OTHER TIPS

Just prepend/append it to the string that rows.xml() returns:

html = '<html><head>...</head><body>' + rows.xml() + '</body></html>'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top