Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of building HTML markup with HTML helpers in web2py? [closed]

Tags:

markup

web2py

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?

like image 777
Mert Nuhoglu Avatar asked Nov 11 '11 08:11

Mert Nuhoglu


1 Answers

First, even in your second example, you should do:

<a href="{{=URL('vote', args=['up', item.id])}}">
<img src="{{=URL('static', 'up.png')}}"/></a>

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 specify request or to use the c and f 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:

{{=TABLE([TR(row['f1'], row['f2'], row['f3']) for row in rows])}}

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 special value argument to set the current value, and automatically handle setting the checked and selected 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 -- the TR() automatically converts the individual elements into separate TD'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.

like image 147
Anthony Avatar answered Nov 04 '22 04:11

Anthony