HTML::Mason
                            by example
                                BPW2007
                              c...
embed perl in text
                            (and then some)


Saturday 27 October 2007                        2
“alternatives”
                    • Text::Template & HTML::Template
                    • Template Toolkit
              ...
building large
                           dynamic websites


Saturday 27 October 2007                      4
• Amazon
                           • IMDB
                           • del.icio.us
                           • DynDNS
  ...
Apache + mod_perl
                    Apache2 + mod_perl2


Saturday 27 October 2007                  6
httpd.conf
                              #PerlModule Apache2::Compat
                           PerlModule HTML::Mason::Ap...
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, <% $pl...
output

                           Hello, World!




Saturday 27 October 2007                   12
Greetings, <% (quot;Earthlingsquot;, quot;Martiansquot;)[rand 2] %>




Saturday 27 October 2007                          ...
in-line perl sections
                                  <%perl>
                                my $noun = 'World';
      ...
Mason pushes the
                  boundaries of the term
                   “Templating System”


Saturday 27 October 200...
“components”:
                           modular design
                              elements


Saturday 27 October 2007 ...
header.mas
                             <html>
                           <head>
                           <title>Welcome...
footer.mas
                             <center><a href=quot;/quot;>Home</
                             a>
               ...
mainpage.html
                             <& header.mas &>

                           this is body text...

            ...
mainpage.html
                      <& header.mas, head => quot;Wally World Homequot;
                      &>

          ...
header.mas
                     <%args>
                   $head
                   </%args>
                   <html>
   ...
autohandler
                    <html>
                 <head>
                   <title><% $m->base_comp->attr('head') %>...
mainpage.html
                    <%attr>
                     head => quot;Wally World Homequot;
                  </%att...
dhandlers
              e.g. http://myserver/newsfeeds/LocalNews/Story1



              /newsfeeds/LocalNews/Story1     =...
components ~ subs
                   <%init>
                    my $ua = $r->header_in('User-Agent');
                   ...
dhandlers vs autohandlers
                    •      use an autohandler when you have a set
                           of ...
Mason Request Objects
                      Two global per-request objects are available to all components: $r
           ...
http://masonhq.com/



Saturday 27 October 2007                         28
http://masonbook.com/



Saturday 27 October 2007                  29
thank you



Saturday 27 October 2007               30
Upcoming SlideShare
Loading in...5
×

HTML::Mason by example

11,818

Published 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 utility- and sysadmin-related tasks.

Talk given at Belgian Perl Workshop 2007 (27 Nov 2007)

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
No Downloads
Views
Total Views
11,818
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
110
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

HTML::Mason by example

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

    Clipping is a handy way to collect important slides you want to go back to later.

×