librelist archives

« back to archive

URL matching in the template

URL matching in the template

From:
Sean Davis
Date:
2012-09-26 @ 16:41
I have a simple nav-bar and would like to assign a class="active" to
the active navigation item based on the URL.  I know I can pass the
request object to the template in each render_template, if necessary,
but I thought there might be another less redundant way of getting
this information and then checking if that path matches the url_for()
in the menu item.  I may easily have missed something obvious in the
docs.

Thanks,
Sean

Re: [flask] URL matching in the template

From:
Jesse Dubay
Date:
2012-09-26 @ 16:57
> I know I can pass the request object to the template in each
> render_template, if necessary,

Good news, the request object is a template global, so you don't need
to explicitly pass it to render_template:
http://flask.pocoo.org/docs/templating/#standard-context

The recommended way to do this is to check the value of
request.endpoint. There's a bit more information in this thread:

http://flask.pocoo.org/mailinglist/archive/2011/7/29/styling-the-current-navigation-element/

Jesse

Re: [flask] URL matching in the template

From:
Sean Davis
Date:
2012-09-26 @ 17:11
On Wed, Sep 26, 2012 at 12:57 PM, Jesse Dubay <jesse@thefortytwo.net> wrote:
>> I know I can pass the request object to the template in each
>> render_template, if necessary,
>
> Good news, the request object is a template global, so you don't need
> to explicitly pass it to render_template:
> http://flask.pocoo.org/docs/templating/#standard-context
>
> The recommended way to do this is to check the value of
> request.endpoint. There's a bit more information in this thread:
> 
http://flask.pocoo.org/mailinglist/archive/2011/7/29/styling-the-current-navigation-element/

Thanks, Jesse.  As I should have guessed, it's all there.

Sean