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.