在 web2py 中使用 HTML 帮助器构建 HTML 标记有什么好处?
我想了解在 web2py 中使用 HTML 帮助程序而不是纯 HTML 标记元素的好处。作为示例,我在 web2py 应用程序中阅读了以下 HTML 标记生成器代码(略带红色):
{{=A(IMG(_src=URL(r=request,c='static',f='up.png')),_href=URL(r=request,f='vote',args=['up',item.id]))}}
通过纯 HTML 标记编写此代码会导致以下结果:
<a href="/reddish/default/vote/up/{{=item.id}}"><img src="/reddish/static/up.png"/></a>
我发现纯 HTML 标记更易于阅读。但我想了解使用 HTML 助手有什么好处吗?
顺便说一句,我第一眼看到的一个好处是 HTML 帮助程序代码不需要显式地编写应用程序的名称,即 reddish
。除此之外,使用 HTML 助手还有其他好处吗?
I would like to learn the benefits of using HTML helpers in web2py instead of plain HTML markup elements. As an example, I read the following HTML markup builder code in a web2py application (reddish):
{{=A(IMG(_src=URL(r=request,c='static',f='up.png')),_href=URL(r=request,f='vote',args=['up',item.id]))}}
Writing this code by plain HTML markup results in this:
<a href="/reddish/default/vote/up/{{=item.id}}"><img src="/reddish/static/up.png"/></a>
I find the plain HTML markup easier to read. But I would like to learn if there are any benefits of using HTML helpers?
By the way, one benefit that I can see right at the first sight is that HTML helper code doesn't require writing the name of the app, namely reddish
, explicitly. Apart of this, are there any other benefits of using HTML helpers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,即使在第二个示例中,您也应该这样做:
始终使用
URL()
帮助器来编写 URL,即使在视图中也是如此。它处理传出重写,并且知道当前应用程序(和控制器),因此您不必包含这些内容(这意味着如果您更改它们的名称,所有 URL 仍然有效)。此外,不再需要指定request
或使用c
和f
关键字(reddish 是一个旧应用程序)。关于助手,一般的经验法则是在您需要在控制器、模型或模块中创建或操作 HTML 的情况下使用它们,但在视图中使用常规 HTML 标记(与 Python 混合,如第二个示例中所示) 。然而,一个例外是当您需要在视图中构建复杂的 HTML 结构时。在这种情况下,在单个 Python 代码块中操作助手可能比将常规 HTML 标签与 Python 混合起来更容易。例如,如果您的字典列表中有数据(带有键“f1”、“f2”、“f3”)并且想要创建一个 HTML 表,您可以执行以下操作:
您可以混合 HTML 和 Python 执行相同的操作,但它会更长、更混乱。
帮助器的另一个主要好处是它们创建了一个 server- side DOM 可以在 Python 代码中进行操作。您最有可能在模型/控制器代码中创建的帮助程序是表单(通过 FORM、SQLFORM、Crud 和 Auth 系统)和表(通过 SQLTABLE、SQLFORM.grid 和 Crud)。这些助手本身包含其他助手,例如表、div 和 ul,它们可以在序列化为 HTML 之前在服务器端进行操作 (例如)。
此外,一些帮助程序具有添加功能或使其更易于使用的特殊功能:
A
:与相同,但也采用处理 Ajax 回调的特殊参数。
HTML
:自动添加文档类型字符串。INPUT
、OPTION
:采用特殊的value
参数来设置当前值,并自动处理设置checked
和分别选择
属性。OL
、UL
、SELECT
、TR
、TBODY
:与 HTML 对应项一样,除非它们自动将不是辅助函数的未命名参数(或列表/元组的元素)转换为适当的子元素(即、
最后,还有许多帮助器具有专门功能的:
BEAUTIFY
:构建复合对象的 HTML 表示。CODE
:MARKMIN
:将 markmin 代码转换为 HTML。MENU
:从嵌套的元组列表创建嵌套的 ul。TAG
:通用标签生成器和 HTML 解析器。XML
:封装不应转义或应清理的文本。有关更多详细信息,请参阅帮助程序文档。
First, even in your second example, you should do:
Always use the
URL()
helper to write URLs, even in the views. It handles outgoing rewrites, and it knows the current app (and controller), so you don't have to include those (which means if you change their names, all the URLs will still work). Also, there is no longer any need to specifyrequest
or to use thec
andf
keywords (reddish is an old app).Regarding helpers, the general rule of thumb is to use them in case you need to create or manipulate HTML in a controller, model, or module, but to use regular HTML markup (mixed with Python, as in your second example) in the views. One exception, though, is when you need to build a complex HTML structure in the view. In that case, it might be easier to manipulate helpers in a single block of Python code rather than intermixing regular HTML tags with Python. For example, if you have data in a list of dictionaries (with keys 'f1', 'f2', 'f3') and want to create an HTML table, you can do:
You could do the same thing mixing HTML and Python, but it would be a lot longer and messier.
Another major benefit of the helpers is that they create a server-side DOM that can be manipulated in your Python code. The helpers you are most likely to create in your model/controller code are forms (via FORM, SQLFORM, Crud, and the Auth system) and tables (via SQLTABLE, SQLFORM.grid, and Crud). These helpers themselves contain other helpers, such as tables, divs, and ul's, and they can be manipulated server side before being serialized into HTML (for example).
Also, some of the helpers have special features that add functionality or make them easier to use:
A
: Same as<a></a>
, but also takes special arguments to handle Ajax callbacks.HTML
: Automatically prepends the doctype string.INPUT
,OPTION
: Take a specialvalue
argument to set the current value, and automatically handle setting thechecked
andselected
attributes, respectively.OL
,UL
,SELECT
,TR
,TBODY
: Like their HTML counterparts, except they automatically convert un-named arguments (or elements of a list/tuple) that are not helpers into the appropriate child elements (i.e.,<li>
,<option>
,<td>
, and<tr>
, respectively). (The table example above makes use of that fact -- theTR()
automatically converts the individual elements into separateTD
's.)Finally, there are a number of helpers that have specialized functionality:
BEAUTIFY
: Builds an HTML representation of compound objects.CODE
: An alternative to<pre></pre>
that automatically handles code highlighting and line numbering for several programming languages.MARKMIN
: Converts markmin code to HTML.MENU
: Creates nested ul's from a nested list of tuples.TAG
: Universal tag generator and HTML parser.XML
: Encapsulates text that should not be escaped or that should be sanitized.See the helpers documentation for more details.