building urls

16 views
Skip to first unread message

mla

unread,
Mar 8, 2014, 2:34:14 PM3/8/14
to perl...@googlegroups.com
I see how Kelp::Routes provides build() to generate URLs from the routes, but how do you typically use that within templates?
Do you build the URLs within the controller and pass them to the template?


Stefan Geneshky

unread,
Mar 8, 2014, 9:05:07 PM3/8/14
to perl...@googlegroups.com
If you're using an advanced template engine, such as Template::Toolkit, you could just pass the response object to the template, then build your URLs within it.

sub my_route {
    my $app = shift
    $app->res->template('my_template', { r => $app->res });
}
Then, in your template you could do something in the lines of:
<a href="[% r.url('home') %]">Homepage</a>
Stefan

mla

unread,
Mar 8, 2014, 11:50:30 PM3/8/14
to perl...@googlegroups.com


On Saturday, March 8, 2014 6:05:07 PM UTC-8, Stefan Geneshky wrote:
If you're using an advanced template engine, such as Template::Toolkit, you could just pass the response object to the template, then build your URLs within it.

sub my_route {
    my $app = shift
    $app->res->template('my_template', { r => $app->res });
}
Then, in your template you could do something in the lines of:
<a href="[% r.url('home') %]">Homepage</a>

Thanks. I ended up assigning the app reference to the template object and defining an AUTOLOAD for <url-name>_path.
That way I have nice rails-style url helper calls like:

  <%= $self->home_path %> 
  <%= $self->user_path(id => 1024) %>
Reply all
Reply to author
Forward
0 new messages