Mojolicious: An interview with author Sebastian Riedel

Tags:

We are experiencing Perl's most successful period since the 1990s. I-Programmer spoke with Sebastian Riedel, originator of the relatively new Mojolicious system built on Perl.

…The truth is that installing Mojolicious is simply a very fast and pleasant experience… we've done with tight integration of WebSockets and event loops, [and] the ability to grow from single-file prototypes to well structured web applications.

The first example application you encounter on

[

our website ](http://mojolicio.us) … doesn't look very complicated at all. But once you start digging a little deeper, you'll quickly realize how crazy (in a good way) it really is, and how hard it would be to replicate with any other web framework, in any language…

Read the rest of the story at i-programmer, 11 December 2014.

Mojo: Content boxes like "Widgets"

Tags:

When we create a sidebar, generally we would like to suppress emitting the wrapper and title of our "widgets" (yes, I am still coming from the view of a developer too long having done nothing but WordPress) when there is no content "in the box."

In code, we can examine the currently defined stash data or content areas like this:

if (length($c->content('content')) { … }

Note that, at least as of Mojolicious 6.0, content() will actually create an empty content area even if not given any content, so we must test length(); for stash data, we probably would use defined instead.

We can do this inside a template too. Here's an example for both content and stash:

#!/usr/bin/env perl

use Mojolicious::Lite;

# setup base route
any '/' => sub {
  my $self = shift;

  # Example stash data and content
  $self->stash( text2 => 'some text' );
  $self->content( area3 => 'some text' );
  $self->render('index');
};

app->start;

__DATA__

@@ index.html.ep

<!DOCTYPE html>
<html>
<head><title>Widget Boxes</title></head>
<body>

  % if (defined stash('text1')) {
      <b><%= stash('text1') %></b>
  % } else {
      <em>No content</em>
  % }

<hr width='50%'>
  % if (defined stash('text2')) {
      <b><%= stash('text2'); %></b>
  % } else {
      <em>No content</em>
  % }

<hr>

  % if (length content('area1')) {
      <b><%= content('area1') %></b>
  % } else {
      <em>No content</em>
  % }
<hr width='50%'>

  % if (length content('area2')) {
      <b><%= content('area2') %></b>
  % } else {
      <em>No content</em>
  % }
<hr width='50%'>

  % if (length content('area3')) {
      <b><%= content('area3') %></b>
  % } else {
      <em>No content</em>
  % }

</body>
</html>

Zero to Mojolicious, Part 3: Using External Content from WP-API

Tags:

Last time, we created a site_design template for Mojolicious which built on a basic layout template for Zurb Foundation. On top of this we can use our content-specific templates, with the two lower templates nested below.

Here's an example that uses content from a WordPress™ Post. The simplest way to retrieve the actual content of just a post from WordPress is to use the rather new WP-API. This was to be part of WP core in their version 4.1, and although its appearance in core has been delayed, can be installed as the "WP REST API" plugin from the WP repository.

Once the WP-API plugin is enabled, we can request a URI like one on this site as:

http://blog.wlindley.com/wp-json/wp/posts/1265

That will return a JSON result with the rendered HTML (WordPress stores post and page content in the database with newline breaks instead of paragraph tags, and an assortment of other changes we do not want to deal with here) as well as a variety of metadata about the post. Unfortunately the current WP-API plugin does not let us request by post name, only by internal ID. One hopes the API will be more complete when it is fully released. Also beware the URI and request schema may differ in a final release.

We can use Mojo::UserAgent to retrieve the remote content and parse its JSON.

In a new file lib/MyApp/Controller/ then we add a controller. We use the rendered HTML versions for the content, title, and excerpt, while copying the other values with unmodified keys all to the stash, which we can then reference in our template. The two templates notfound and nopage (not shown here) are used when things go wrong.

package MyApp::Controller::Blogpage;
use Mojo::Base 'Mojolicious::Controller';

use Data::Dumper;
use Mojo::UserAgent;

# This action will render a template
sub retrieve {
  my $self = shift;

  my $ua = Mojo::UserAgent->new;

  my $id = $self->param('id');

  if (defined $id) {
      $id =~ s/\D//g; # remove all non-digits
  }
  if (!$id) {
      $self->redirect_to('notfound');
      return;
  }

  my $tx = $ua->get("http://blog.wlindley.com/wp-json/wp/posts/${id}");

  if ($tx->success) {
      my $value = $tx->res->json;

      # Render template "blogpage/retrieve.html.ep" with
      # values from WP-API
      $self->render((map { $_ => $value->{$_}->{rendered} }
                           qw/content title excerpt/) ,
                    (map { my $vname = $_;
                           $vname =~ s/^format\Z/wp_format/;
                           $vname => $value->{$_} }
                           qw/date type format comment_status id modified
                                ping_status featured_image author sticky
                                excerpt slug guid link/) );
  } else {
      $self->redirect_to('nopage');
  }  
}

1;

and create a template in templates/blogpage/retrieve.html.ep. Note how we are using the site_layout template from last time:

% layout 'site_layout';
% title "$title";
<h1><%= $title =%></h1>

<blockquote>Published on <%= $date %></blockquote>
%== $content

Of course you can use any of the metadata tags which we copy in the controller (featured_image might be an interesting one).

Then in our MyApp.pm we add this into the startup subroutine:

$r->get('/blog')->to('blogpage#retrieve');

Now we can visit http://localhost:3000/blog?id=1265 and you should see the content of the WordPress post, dropped into the content section that you made in the Foundation style theme from last time.

Next time, we still plan to access a database.

Testing Mojolicious locally

Tags:

So you have coded your newest web system in Mojolicious and would like to test it in an environment where it "thinks" it's running on the actual webserver, even though it's still running on your machine.

Add your www.example.com entry to point to 127.0.0.1 in /etc/hosts and then redirect your localhost's port 80 to port 3000:

sudo iptables -t nat -I OUTPUT -p tcp     -d 127.0.0.1 --dport 80     -j REDIRECT --to-ports 3000

No need to run as root!

A Bad Day for Freedom, Science, and Technology

Tags:

"Net Neutrality" sounds good, like Swiss chocolate. But…

Now the FCC will begin to tax the Internet and dictate how the Web develops.

Remember Ma Bell?

Telephone answering machines were invented in the 1930s but only introduced to the public in the 1970s, because it was not permitted by Tariffs and other Regulations.

Packet switching, which is the foundation of the Internet, was invented in the late 1950s but held back from wide use until the late 1980s, because it was not permitted by Tariffs and other Regulations.

Government over-regulation held back the Internet for half a century! Imagine what America could have been if we had the Web in the 1960s. But we didn't, because of the FCC and its friends.

Mark my words: This week's ruling spells the end of freedom and quashes the development of science and technology.

Be careful what you wish for.