On Wed, Jan 7, 2009 at 12:05 PM, Wyatt Baldwin
<[email protected]> wrote:
>
> I'd say `h` is intended as a convenient namespace for your Web-related
> utilities (AKA "helpers"), which are mainly used in templates, but I
> can imagine cases where you might want to use those helpers in a
> controller.
>
> If you just need `url_for`, you could also import it directly from
> Routes:
>
>    from routes import url_for

Many of the tutorials on the wiki are old, and the best practices have
evolved over time,  so mk's confusion is understandable.

By definition, a helper is a generic function that's useful in
templates and/or controllers.  'h' is automatically available in
templates.  In controllers you have to do "import myapp.lib.helpers as
h".

In Pylons 0.9.5, 'h' was automatically available in controllers via an
import in the base controller module.  This was eliminated because of
dissastisfaction with "import *".

url_for has traditionally been in the helpers module following a Rails
tradition.  Pylons 0.9.7 introduces pylons.url ('url' in templates),
as an eventual replacement for url_for.  That does not need to be
accessed via 'h'.

Most helpers are not application specific, but some applications may
wish to define application-specific helpers.  For instance, I have a
format_date() function so that all dates are displayed in a consistent
way.  Helpers can also be used to expand numeric identifiers to their
string equivalents, or for latitude/longitude formatting, etc.  If you
use 'literal("<br />")' a lot, you can define a BR constant in the
helpers module.  Things like that.

-- 
Mike Orr <[email protected]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to