web2py - 如何注入 html [英] web2py - how to inject html

查看:27
本文介绍了web2py - 如何注入 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用rows.xml() 来生成html 输出.我想知道如何向这个生成的 html 页面添加 html 代码,例如:添加徽标、链接 css 文件等"

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)返回行.xml()

推荐答案

有很多 HTML helper 可以使用,例如:

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

等等.

您甚至可以自动创建整个表格:

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.

它非常强大,通常您不必费心自己编写实际的 HTML.这里是备忘单,或者您可以直接查看网站文档.

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.

为了确保您实际上不需要生成整个 HTML 页面,让 web2py 将您的响应插入到与您的控制器同名的模板中会更容易(或强制使用特定模板response.view = 'template.html'.文档教程将更好、更详细地解释这一点.

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.

简而言之,如果您正在实现函数 index,您可以返回一个字符串(整个页面 HTML,这似乎是您要查找的内容)或字典使用模板.

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

否则,在 views/controller/index.html 中编写一个 html 模板(如果您在函数中插入 response.view=...,则为另一个文件,重新使用相同的模板),这可能是这样的:

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>

并返回一个字典:

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

这篇关于web2py - 如何注入 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆