HTML::Mason by example
Upcoming SlideShare
Loading in...5
×
 

HTML::Mason by example

on

  • 14,070 views

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 ...

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)

Statistics

Views

Total Views
14,070
Views on SlideShare
14,041
Embed Views
29

Actions

Likes
5
Downloads
107
Comments
1

5 Embeds 29

http://www.slideshare.net 24
http://bipmoldo.bipmoldova.com 2
http://localhost 1
http://paper.li 1
https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

HTML::Mason by example HTML::Mason by example Presentation Transcript

  • HTML::Mason by example BPW2007 chrisv.cpan.org Saturday 27 October 2007 1
  • embed perl in text (and then some) Saturday 27 October 2007 2
  • “alternatives” • Text::Template & HTML::Template • Template Toolkit • Apache::ASP • Embperl • PHP • ASP (ActiveState ActivePerl) Saturday 27 October 2007 3
  • building large dynamic websites Saturday 27 October 2007 4
  • • Amazon • IMDB • del.icio.us • DynDNS • jobs.perl.org • RT Saturday 27 October 2007 5
  • Apache + mod_perl Apache2 + mod_perl2 Saturday 27 October 2007 6
  • httpd.conf #PerlModule Apache2::Compat PerlModule HTML::Mason::ApacheHandler <LocationMatch quot;.html$quot;> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler </LocationMatch> Saturday 27 October 2007 7
  • preload perl modules startup.pl Saturday 27 October 2007 8
  • Apache integration (objects) Saturday 27 October 2007 9
  • by example Saturday 27 October 2007 10
  • sample “component” % my $planet = quot;Worldquot;; Hello, <% $planet %>! Saturday 27 October 2007 11
  • output Hello, World! Saturday 27 October 2007 12
  • Greetings, <% (quot;Earthlingsquot;, quot;Martiansquot;)[rand 2] %> Saturday 27 October 2007 13
  • in-line perl sections <%perl> my $noun = 'World'; my @time = localtime; </%perl> Hello <% $noun %>, % if ( $time[2] < 12 ) { good morning. % } else { good afternoon. % } Saturday 27 October 2007 14
  • Mason pushes the boundaries of the term “Templating System” Saturday 27 October 2007 15
  • “components”: modular design elements Saturday 27 October 2007 16
  • header.mas <html> <head> <title>Welcome to Wally World!</title> </head> <body bgcolor=quot;#CCFFCCquot;> Saturday 27 October 2007 17
  • footer.mas <center><a href=quot;/quot;>Home</ a> </center> </body></html> Saturday 27 October 2007 18
  • mainpage.html <& header.mas &> this is body text... <& footer.mas &> Saturday 27 October 2007 19
  • mainpage.html <& header.mas, head => quot;Wally World Homequot; &> this is body text... <& footer.mas &> Saturday 27 October 2007 20
  • header.mas <%args> $head </%args> <html> <head> <title><% $head %></title> </head> <body bgcolor=quot;#CCFFCCquot;> <center><h1><% $head %></h1></center> Saturday 27 October 2007 21
  • autohandler <html> <head> <title><% $m->base_comp->attr('head') %></title> </head> <body bgcolor=quot;#CCFFCCquot;> <center> <h1><% $m->base_comp->attr('head') %></h1> </center> % $m->call_next; <center><a href=quot;/quot;>Home</a></center> </body></html> Saturday 27 October 2007 22
  • mainpage.html <%attr> head => quot;Wally World Homequot; </%attr> this is body text... Saturday 27 October 2007 23
  • dhandlers e.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) Saturday 27 October 2007 24
  • components ~ subs <%init> my $ua = $r->header_in('User-Agent'); return ($ua =~ /Mozilla/i && $ua !~ /MSIE/i) ? 1 : 0; </%init> Saturday 27 October 2007 25
  • 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 Saturday 27 October 2007 26
  • Mason Request Objects Two 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]) Saturday 27 October 2007 27
  • http://masonhq.com/ Saturday 27 October 2007 28
  • http://masonbook.com/ Saturday 27 October 2007 29
  • thank you Saturday 27 October 2007 30