HTML::Mason by example

  • 11,229 views
Uploaded on

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)

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
11,229
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
110
Comments
1
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. HTML::Mason by example BPW2007 chrisv.cpan.org Saturday 27 October 2007 1
  • 2. embed perl in text (and then some) Saturday 27 October 2007 2
  • 3. “alternatives” • Text::Template & HTML::Template • Template Toolkit • Apache::ASP • Embperl • PHP • ASP (ActiveState ActivePerl) Saturday 27 October 2007 3
  • 4. building large dynamic websites Saturday 27 October 2007 4
  • 5. • Amazon • IMDB • del.icio.us • DynDNS • jobs.perl.org • RT Saturday 27 October 2007 5
  • 6. Apache + mod_perl Apache2 + mod_perl2 Saturday 27 October 2007 6
  • 7. 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
  • 8. preload perl modules startup.pl Saturday 27 October 2007 8
  • 9. Apache integration (objects) Saturday 27 October 2007 9
  • 10. by example Saturday 27 October 2007 10
  • 11. sample “component” % my $planet = quot;Worldquot;; Hello, <% $planet %>! Saturday 27 October 2007 11
  • 12. output Hello, World! Saturday 27 October 2007 12
  • 13. Greetings, <% (quot;Earthlingsquot;, quot;Martiansquot;)[rand 2] %> Saturday 27 October 2007 13
  • 14. 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
  • 15. Mason pushes the boundaries of the term “Templating System” Saturday 27 October 2007 15
  • 16. “components”: modular design elements Saturday 27 October 2007 16
  • 17. header.mas <html> <head> <title>Welcome to Wally World!</title> </head> <body bgcolor=quot;#CCFFCCquot;> Saturday 27 October 2007 17
  • 18. footer.mas <center><a href=quot;/quot;>Home</ a> </center> </body></html> Saturday 27 October 2007 18
  • 19. mainpage.html <& header.mas &> this is body text... <& footer.mas &> Saturday 27 October 2007 19
  • 20. mainpage.html <& header.mas, head => quot;Wally World Homequot; &> this is body text... <& footer.mas &> Saturday 27 October 2007 20
  • 21. 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
  • 22. 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
  • 23. mainpage.html <%attr> head => quot;Wally World Homequot; </%attr> this is body text... Saturday 27 October 2007 23
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. http://masonhq.com/ Saturday 27 October 2007 28
  • 29. http://masonbook.com/ Saturday 27 October 2007 29
  • 30. thank you Saturday 27 October 2007 30