Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Mojolicious absolute url with query in template?

by Anonymous Monk
on Mar 19, 2020 at 04:44 UTC ( [id://11114451]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This works but seems wrong:
<%= url_for('/')->to_abs %>?<%= url_with->query %>
Is there a better way?

Replies are listed 'Best First'.
Re: Mojolicious absolute url with query in template?
by haukex (Archbishop) on Mar 19, 2020 at 07:09 UTC
    This works but seems wrong: <%= url_for('/')->to_abs %>?<%= url_with->query %> Is there a better way?

    Absolutely!

    <%= url_for('/')->to_abs->query(foo=>"bar") %>
      Thanks for replying. I tried adding ->query before asking the question, because it seemed logical, but it didn't work as expected. This displays the url without the query string:
      <%= url_for('/')->to_abs %>
      But this displays absolutely nothing:
      <%= url_for('/')->to_abs->query %>
      Here's the weird part, a link made with "to_abs->query" works, but can't be right clicked ("Void" link with click handler) and when source is viewed the href appears to be blank:
      <a href="<%= url_for('/')->to_abs->query %>"> Reload </a>
      What is this voodoo?

        You don't give any argument to ->query(), so the query part is empty. If you want query parameters, you have to give them to the method, like haukex already demonstrated.

        <a href="<%= url_for('/')->to_abs->query( foo => 'bar' )%>"> Reload </ +a>

        If you want to keep the current query parameters, you need to pass those into your template.

        Oh, I think I see what you're getting at. Note the documentation for url_with:

        Does the same as "url_for", but inherits query parameters from the current request.

        <update> Expanded url_for vs. url_with examples. </update>

        #!/usr/bin/env perl use Mojolicious::Lite -signatures; get '/' => sub ($c) { if ( !$c->param('foo') ) { $c->redirect_to( $c->url_for('index')->query(foo=>"bar") ) } else { $c->render('index') } } => 'index'; app->start; __DATA__ @@ index.html.ep % layout 'main', title => 'Hello, World!'; <pre> %= url_for('/') %= url_for('/')->to_abs %= url_for('/')->to_abs->query(abc=>"xyz") %= url_with('/') %= url_with('/')->to_abs %= url_with('/')->to_abs->query(abc=>"xyz") </pre> @@ layouts/main.html.ep <!DOCTYPE html> <html> <head><title><%= title %></title></head> <body><main> <%= content %> </main></body> </html>

        Visiting http://localhost:3000/ results in a single redirect (to add the query for demonstration purposes), and then renders as:

        / http://localhost:3000/ http://localhost:3000/?abc=xyz /?foo=bar http://localhost:3000/?foo=bar http://localhost:3000/?abc=xyz

        Also, note the link_to helper: %= link_to Reload => url_with('/')->to_abs results in <a href="http://localhost:3000/?foo=bar">Reload</a>

Re: Mojolicious absolute url with query in template?
by Anonymous Monk on Mar 19, 2020 at 06:37 UTC
    Sure
    helper foo => sub {...}; __END__ <%= my_foo() =%>
    and then my_helper_foo() is as simple or as complicated as you need it to be
      I have no idea what you're talking about but it reminds me of the documentation for Mojolicious (confusing, yet confident). How is that going to display the request url?

        Confused is as confused does

        #!/usr/bin/perl -- use Mojolicious::Lite; helper my_foo => sub { my( $c ) = @_; $c->url_for('/')->to_abs.'?'.$c->url_with->query; }; any '/' => { template => 'index', time => scalar localtime };;; app->start; __DATA__ @@ index.html.ep <%= $time =%> <pre> <%= url_for('/')->to_abs %>?<%= url_with->query %> <%= my_foo() =%> __END__
      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114451]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-29 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found