On Jan 7, 9:05 am, mk <mrk...@gmail.com> wrote:
> Hello everyone,
>
> I'm really new to Pylons, so could someone please help me clear some of
> the confusion with the following stuff?
>
> I'm trying to access global h variable in Pylons 0.9.7, mainly for sake
> of getting at url_for function.
>
> But h var seems to be available only in Mako templates and not the
> controller code (at least in 0.9.7).
>
> http://wiki.pylonshq.com/display/pylonscookbook/Routes+for+people+in+...
> contains example of using h variable without quoting the context, but it
> may look like controller code (or routing.py).
>
> So my question is, what is h variable mainly for? Should I use it in
> (mainly or only in) templates or are there uses for it in controller
> code? If so, how to access it there?
>
> Regards,
> mk

By default, Pylons "injects" `h` into the template context for you.

To get access to `h` in a controller:

    from pylons import config
    h = config['pylons.h']

See config/environment.py for where `config['pylons.h']` is
configured. By default, it points at the <yourapp>.lib.helpers module,
so you could also get `h` in a controller like this:

    import <yourapp>.lib.helpers as h

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

Hope that helps a bit.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to