Echo text with Bailador using Template::Mojo

After getting started with Bailador serving simple text, we saw how we can accept input from the user and echo it back, but we had the HTML snippets embedded in our Perl 6 code. That can work for really small applications, but if we would like to build a real web application we need to move the HTML out to external template files.

Bailador uses Template::Mojo as its default templating system.

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.

Bailador web application

In our example we use two files. One of them is the main Perl 6 script called app.pl6 and the other one is the views/ located in the views subdirectory:


use v6;
use Bailador;

get '/' => sub {
    return '<a href="/echo">Echo</a>';

get '/echo' => sub {
	template '';

post '/echo' => sub {
	template '', {text => request.params<text>};


The application has 3 routes. The / route returns an HTML snippet still embedded in the Perl 6 code, displaying a link to the /echo page.

Then there are two /echo routes. One that will handle the GET request to this URL, that returns the content of the template, the other one that will pass the value of the text field of the HTML form to the template engine.

One important thing to mention. In order to know where the root of the application is and from there to deduct where the views directory is, Bailador makes some calculations in its import function. As of this writing Rakudo 2015.07 does not automatically call this function thus after loading Bailador we had to include a call:



The template itself is basically a subroutine in which the first line accepts the parameters from the call to the template function. (Except that the name of the template file is not passed to the template engine. Only the hash after it.

% my ($h) = @_;

Lines in the template that start with % are considered Perl 6 code. Snippets that that are embedded in <%= %> tags are values interpolated in the HTML document:

% if $h<text> {
	You said <%= $h<text> %>
% }

The full template looks like this.


% my ($h) = @_;

<form method="POST" action="/echo"><input name="text"><input type="submit"></form>

% if $h<text> {
	You said <%= $h<text> %>
% }

We can run perl6 app.pl6 which will launch a small web server on port 3000. We can then visit and follow the link to Echo where we'll see the form to be filled.

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-07-30


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.81189517

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.