Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Perl Dancer - from installation to your first real application

Previous article - Perl Dancer - Installation and first test. Summary of that article - What is Dancer / How do I install it / Installing requires CPAN modules / Creating an application / testing that it worked.

The application in the first article provides you with no more that a welcome page to tell you that you have installed Perl Dancer and it's functioning. This article is the second step - telling you how to add your own content within the framework.

Adding in your own route and code

Modify bin/app.pl to read:

  #!/usr/bin/env perl
  use Dancer;
  use MyWeb::App;
  
  sub hometown {
    return "What a great place to live!";
    }
  
  get '/town/melksham.html' => \&hometown;
  
  dance;


And that (yes, it really is that easy to start!) is your first application.

The call to the get function is your route, telling the web server to send any requests for the /town/melksham.html URL to the hometown function.

sub hometown is your rudimentary controller, simply returning a string of HTML that's to be rendered.

And it looks like this:



A little more routing - a whole family of pages to a single controller

If you want to add a whole pattern of pages - support multiple URLs through a single controller - you can do so by adding a named parameter to your router:

  get '/town/:where.html' => \&awaytown;

and picking up that parameter through the params method in the controller method - for example

  sub awaytown {
    return params->{where} . " is quite nice but ... ".
      "have you thought of moving to ".
      "<a href=/town/melksham.html>Melksham</a>?";
    }



 

 


Adding in a template

You'll note that I've been referring to our hometown and awaytown methods as controllers, and yet they've contained the look and feel elements (the view). Let's move that view out into a separate template, so that we can start to separate out the business logic from the look and feel.

  sub hometown {
    my $saying = "<h4>What a great place to live!&t;/h4>".
      "Come along to <a href=http://melksh.am/hotel>".
      "Well House Manor</a> and we'll welcome you";
    my %fillings = (dynamics => $saying, pagename => "melksham");
    template ('township.tt',\%fillings);
    }


Alas, I then find I've got another dependency issue:

  munchkin:bin grahamellis$ ./app.pl
  Can't locate Template.pm in @INC (@INC contains: /Users/grahamellis/Dancer-1.3113/MyWeb-App/lib /Library/Perl/5.12/darwin-thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.3/darwin-thread-multi-2level /Library/Perl/Updates/5.12.3 /System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 /System/Library/Perl/Extras/5.12/darwin-thread-multi-2level /System/Library/Perl/Extras/5.12 .) at ./app.pl line 3.
BEGIN failed--compilation aborted at ./app.pl line 3.


Oops - let's load that from the CPAN:

  munchkin:bin grahamellis$ sudo perl -MCPAN -e shell
  Password:
  cpan[1]> install Template


and run again... viewing the pages gives:



The problem being that we haven't provided the template yet! Let's do so, in file township.tt in the views directory:

  <h1>This is a Township!</h1>
  This line of text is within a template that I've applied right through
  the town application, and filled in with various dynamic fields. You called
  this up under the name "<% pagename %>". All sorts of standard stuff would
  be going here, which would have dynamics merged into it. Here comes the main
  dynamic block:<hr />
  <% dynamics %>
  <hr />
  As well as all the headers, you'll want small print such as<br />
  Copyright <a href=http://www.wellho.net>Well House Consultants</a><br />
  and a link to the top of this app <a href=/town/>[here]</a> or
  search page <a href=/town/search.html>[here]</a>.


You'll note that our template is standard .html, with additional markers between <% and %> markers saying where data is to be inserted. And it'll now work:


 

 


Providing your wrapper template and style

The application's starting to look rather good, even though we've written minimal code. But there's still the Dance logo and copyright statement which we won't want on our production sites - how do I replace / update them?

Dancer applications have an overall, surrounding layout around each and every page - it's a sort of super-template. Upon installation, the file that's used for the outer layout is called layouts/main.tt - and you can either change it or (better) provide a replacement, and point Dancer to that replacement via the config.yml file. In that file, change
  layout: "main"
to
  layout: "wellhouseframe"

And in the wellhouseframe.tt provide something like this:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-type" content="text/html; charset=<% settings.charset %>" />
  <title>This is my page <% pagename %></title>
  <link rel="stylesheet" href="<% request.uri_base %>/css/townstyle.css" />
  </head>
  <body>
  <% content %>
  <div id="footer">
  Written by Graham Ellis of Well House Consultants / Perl Dancer teaching example
  </div>
  </body>
  </html>


Note the use of <% content %> to instruct the templating system where to insert the inner template.

I have modified a copy of main.tt rather than entered this from scratch. Similarly, take the style sheet file that the original installation came with, modify it and save it under the name that's in the layout. Changes I made here included adding in margins all around, changing the background colour, and removing the watermark image. Results...


 


Adding in a form

The final example also included:
* a form in a new template
* collecting information from that form
* redirection to a result page if the form is [correctly] completed, or returning the original form again if not
* Using either the POST or the GET methods to submit that form.



Complete source code for this final example:
./bin/app - the controllers and routes
township.tt - the main template pafe
askuser.tt - the form template
wellhouseframe.tt - the surrounding layout template
config.yml - the configuration for Dancer
townstyle.css - the amended style sheet
(written 2013-05-24, updated 2013-05-25)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P408 - Perl - Standard Web Modules
  [4099] Perl Dancer - a Perl Framework - Installation and first test - (2013-05-23)
  [3485] Perl - retrieving and caching web resources - (2011-10-18)
  [2416] Automating access to a page obscured behind a holding page - (2009-09-23)
  [2402] Automated Browsing in Perl - (2009-09-11)
  [2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)


Back to
Perl Dancer - a Perl Framework - Installation and first test
Previous and next
or
Horse's mouth home
Forward to
POETS day at Well House Manor
Some other Articles
Weymouth vis the TransWilts - the day out today
Questions from children about Melksham Campus
Installing Lua 5.2.2 on Mac OS X 10.7.4
POETS day at Well House Manor
Perl Dancer - from installation to your first real application
Using object orientation for non-physical objects
Melksham Chamber of Commerce - Report for AGM, 21st May 2013
Perl design patterns example
Django - first steps - Updated
4734 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4100_Per ... ation.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat