Top Banner
HTML::Mason by example BPW2007 chrisv.cpan.org 1 Saturday 27 October 2007
30

HTML::Mason by example

Dec 18, 2014

Download

Technology

Mason is a powerful, robust and proven templating system for serving dynamic content on the Web. Mason is most commonly used to build dynamically generated web sites but can also be used for more utility- and sysadmin-related tasks.

Talk given at Belgian Perl Workshop 2007 (27 Nov 2007)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HTML::Mason by example

HTML::Masonby example

BPW2007chrisv.cpan.org

1Saturday 27 October 2007

Page 2: HTML::Mason by example

embed perl in text(and then some)

2Saturday 27 October 2007

Page 3: HTML::Mason by example

“alternatives”

• Text::Template & HTML::Template

• Template Toolkit

• Apache::ASP

• Embperl

• PHP

• ASP (ActiveState ActivePerl)

3Saturday 27 October 2007

Page 4: HTML::Mason by example

building largedynamic websites

4Saturday 27 October 2007

Page 5: HTML::Mason by example

• Amazon

• IMDB

• del.icio.us

• DynDNS

• jobs.perl.org

• RT

5Saturday 27 October 2007

Page 6: HTML::Mason by example

Apache + mod_perlApache2 + mod_perl2

6Saturday 27 October 2007

Page 7: HTML::Mason by example

httpd.conf

#PerlModule Apache2::CompatPerlModule HTML::Mason::ApacheHandler<LocationMatch "\.html$"> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler</LocationMatch>

7Saturday 27 October 2007

Page 8: HTML::Mason by example

preload perl modulesstartup.pl

8Saturday 27 October 2007

Page 9: HTML::Mason by example

Apache integration(objects)

9Saturday 27 October 2007

Page 10: HTML::Mason by example

by example

10Saturday 27 October 2007

Page 11: HTML::Mason by example

sample “component”

% my $planet = "World";Hello, <% $planet %>!

11Saturday 27 October 2007

Page 12: HTML::Mason by example

output

Hello, World!

12Saturday 27 October 2007

Page 13: HTML::Mason by example

Greetings, <% ("Earthlings", "Martians")[rand 2] %>

13Saturday 27 October 2007

Page 14: HTML::Mason by example

in-line perl sections<%perl>

my $noun = 'World'; my @time = localtime;</%perl>Hello <% $noun %>,% if ( $time[2] < 12 ) {good morning.% } else {good afternoon.% }

14Saturday 27 October 2007

Page 15: HTML::Mason by example

Mason pushes the boundaries of the term “Templating System”

15Saturday 27 October 2007

Page 16: HTML::Mason by example

“components”:modular design

elements

16Saturday 27 October 2007

Page 17: HTML::Mason by example

header.mas<html>

<head><title>Welcome to Wally World!</title></head><body bgcolor="#CCFFCC">

17Saturday 27 October 2007

Page 18: HTML::Mason by example

footer.mas<center><a href="/">Home</a>

</center></body></html>

18Saturday 27 October 2007

Page 19: HTML::Mason by example

mainpage.html<& header.mas &>

this is body text...

<& footer.mas &>

19Saturday 27 October 2007

Page 20: HTML::Mason by example

mainpage.html<& header.mas, head => "Wally World Home" &>

this is body text...

<& footer.mas &>

20Saturday 27 October 2007

Page 21: HTML::Mason by example

header.mas<%args>

$head</%args><html><head><title><% $head %></title></head><body bgcolor="#CCFFCC"><center><h1><% $head %></h1></center>

21Saturday 27 October 2007

Page 22: HTML::Mason by example

autohandler<html>

<head> <title><% $m->base_comp->attr('head') %></title></head><body bgcolor="#CCFFCC"><center><h1><% $m->base_comp->attr('head') %></h1></center>% $m->call_next;<center><a href="/">Home</a></center></body></html>

22Saturday 27 October 2007

Page 23: HTML::Mason by example

mainpage.html<%attr>

head => "Wally World Home"</%attr>this is body text...

23Saturday 27 October 2007

Page 24: HTML::Mason by example

dhandlerse.g. http://myserver/newsfeeds/LocalNews/Story1

/newsfeeds/LocalNews/Story1 => no such thing/newsfeeds/LocalNews/dhandler => no such thing/newsfeeds/dhandler => found! (search ends)/dhandler

(The found dhandler would read “LocalNews/Story1” from $m->dhandler_arg and use it as a retrieval key into a database of stories)

24Saturday 27 October 2007

Page 25: HTML::Mason by example

components ~ subs<%init>

my $ua = $r->header_in('User-Agent'); return ($ua =~ /Mozilla/i && $ua !~ /MSIE/i) ? 1 : 0;</%init>

25Saturday 27 October 2007

Page 26: HTML::Mason by example

dhandlers vs autohandlers

• use an autohandler when you have a set of components to handle your pages and you want to augment them with a template/filter

• use a dhandler when you want to create a set of “virtual URLs” that don't correspond to any actual components, or to provide default behavior for a directory

26Saturday 27 October 2007

Page 27: HTML::Mason by example

Mason Request ObjectsTwo global per-request objects are available to all components: $r (provides a Perl API to current Apache request) and $m (provides a Perl API to current Mason request)

$r->uri # the HTTP request URI$r->content_type # set or retrieve content-type

$m->caller() # returns the calling component$m->dhandler_arg() # returns url parts$m->redirect(URL, [HTTP status])

27Saturday 27 October 2007

Page 28: HTML::Mason by example

http://masonhq.com/

28Saturday 27 October 2007

Page 29: HTML::Mason by example

http://masonbook.com/

29Saturday 27 October 2007

Page 30: HTML::Mason by example

thank you

30Saturday 27 October 2007