url_for with target='_blank' - mojolicious

How to create a link with Mojolicious tag-helper url_for which opens in a new window/tab?
The result should be HTML-Code like RBA List

%= link_to 'RBA-List' => url_for('list_rba'), target => '_blank'

Related

How to set up query arguments to `link_to` mojolicious helper?

I want to pass arguments for link generated by link_to helper:
%= link_to 'Change password' => 'auth_remind', { login => $login }
but this does not work.
How to set up query arguments?
It is not documented yet, but in default welcome.html.ep we can found:
%= link_to 'click here' => url_for
So we can do next:
%= link_to 'Change password' => url_for( 'auth_remind' )->query( login => $login )
If we need to set up some arguments for route we may:
%= link_to 'Change password' => url_for( 'auth_remind', { format => 'txt' } )->query( login => $login )

How do I bind to onclick events for buttons in Mojolicious?

I'm trying to make a small webapp to control the command-line music player I use (mocp), but I'm having trouble hitting routes when the user presses a button on the page. I expect the problem is related to my lack of HTML.
Code:
#!/usr/bin/perl
use Mocp;
use Mojolicious::Lite;
my $mocp = Mocp->new;
for my $action ( qw( play pause stop next previous unpause toggle-pause server ) ) {
get $action => sub {
my $self = shift;
$mocp->$action;
$self->render( 'controls' );
};
}
get '/' => sub { shift->render( 'controls' ) };
app->start;
__DATA__
## controls.html.ep
%= input_tag 'play', type => 'button', value => 'Play'
%= input_tag 'pause', type => 'button', value => 'Pause'
%= input_tag 'volup', type => 'button', value => 'Volume Up'
%= input_tag 'voldown', type => 'button', value => 'Volume Down'
%= input_tag 'toggle', type => 'button', value => 'Toggle'
%= input_tag 'next', type => 'button', value => '>>'
%= input_tag 'previous', type => 'button', value => '<<'
If I navigate to the routes directly (e.g. '/play'), everything works as expected. The goal is to simply click the 'Play' button to execute the associated method, rather than having to type the route out by hand. My questions at this stage are:
Is mapping these methods to routes the best way to go about this?
If so, how do I trigger a route with an 'onclick' button event as in JavaScript?
Any advice is much appreciated.
Using jQuery on client side you can trigger route with $.get() function
%= input_tag 'play', type=>'button', value=>'Play', onclick=>"$.get('/play')"
or
<input type="button" value="Play" onclick="$.get('/play');">
To use jQuery you have to add the following line into your template
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

How to make a page-specific title in Dancer templates?

I have a standard Perl Dancer app, using Template::Toolkit as rendering engine, with two routes:
get '/' => sub {
template 'index';
};
get '/foo' => sub {
template 'foo';
};
My views/templates/main.tt contains the line:
<title><%= title %></title>
I want the value of title var be "My Site" on page '/', and "Foo - My Site" on page '/foo'.
I know I can put these values in the controller file, like this:
template 'index', { title => 'My Site' };
but I want to specify them in the corresponding template files, views/index.tt and views/foo.tt.
How can I do that?
Thanks.
This documentation clearly explains how to configure your application to make available in your layout the variables defined in your templates.
For the title tag, you can save yourself the effort to define the title in each template using the variable template.name. Maybe something like that:
<title>
<% template.name.match('(\w+).tt').0.ucfirst %> - <% settings.sitename %>
</title>

How to use Facebook OAuth 2.0 authentication like Zynga and other use it?

I want to display the "Request for Permission" box directly when the user enters http://apps.facebook.com/myfancyapp. The Facebook Authentication documentation is pretty clear on how the URL have to look like
https://graph.facebook.com/oauth/authorize?client_id=[APPID]&redirect_uri=http://www.myfancyapp.com/&scope=user_photos,user_videos,publish_stream
Pasting this URL directly into the browser works like it should. What I want is to redirect the user via JavaScript (or something else) from the application URL
http://apps.facebook.com/myfancyapp
to the authentication box URL above.
I thought something like this would work:
<script type="text/javascript">
<!--
window.location = "https://graph.facebook.com/oauth/authorize?client_id=[APPID]&redirect_uri=http://www.myfancyapp.com/&scope=user_photos,user_videos,publish_stream"
//-->
</script>
This redirects me to a page with a body that looks like this
Clicking on the image/test then redirects to the authentication box.
How can I directly redirect to the "Request for Permission" box. I know it works somehow as other developers (Zynga for example) already do it.
I'm doing the same thing as the asker, only instead of window.location, use top.location.href = "https://graph....."
Like:
<script type="text/javascript">
top.location.href = "https://graph.facebook.com/oauth/authorize?client_id=[APPID]&scope=email&redirect_uri=http://apps.facebook.com/myfancyapp/";
</script>
Prompts for login if needed, and then permissions. Then redirects to the app.
Doing this with PHP is a cinch.
On the backend
$facebook = new Facebook( array(
'appId' => '<FB_APP_ID>'
, 'secret' => '<FB_APP_SECRET>'
, 'cookie' => true
));
$fbSession = $facebook->getSession();
if ( !$fbSession )
{
$url = $facebook->getLoginUrl( array(
'canvas' => 1
, 'fbconnect' => 0
, 'display' => 'page'
, 'cancel_url' => null
, 'req_perms' => 'user_photos,user_videos,publish_stream'
) );
}
And then, on the frontend
<script type="text/javascript">
top.location.href = '<?php echo $url; ?>';
</script>
<p>
Not being redirected? Click Here.
</p>

iFrame App. Permissions Request?

I want to request permissions when the user first clicks my iFrame Facebook application. The problem is the examples I have seen force the user to click a button to load the http://www.facebook.com/authorize.php URL.
Is there a way to iframe the authorize.php page in my application? I've seen it done before but can't find out how.
If I currently try it, it shows the "go to facebook box". The method I seen changes the href or something on the browser.
Any ideas?
I do something like this in one of my iframe applications (I've simplified the actual code for this example)
$fbSession = $facebook->getSession();
if ( $fbSession )
{
$url = $facebook->getLoginUrl( array(
'canvas' => 1
, 'fbconnect' => 0
, 'req_perms' => 'list,of,perms'
, 'display' => 'page'
) );
include( 'redirect.php' );
exit;
}
Then, redirect.php
<script type="text/javascript">
top.location.href = '<?php echo $url; ?>';
</script>
<p>
Not being redirected? Click Here.
</p>
no need to complicate: target="_top" is the answer!
Best!
m