web2py - how to inject html

橙三吉。 提交于 2019-12-06 10:17:35

问题


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()


回答1:


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)



回答2:


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

html = '<html><head>...</head><body>' + rows.xml() + '</body></html>'


来源:https://stackoverflow.com/questions/1550368/web2py-how-to-inject-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!