SlideShare a Scribd company logo
1 of 70
Download to read offline
Mason
A Template System for us programmers




           March 2013 - http://www.eteve.net
A bit of myth busting
●   Mason is old
●   Mason is a viewcontroller
●   Mason is too complex
●   Mason is slow
●   Mason uses embedded Perl
Mason is old
Mason 1 is old (HTML::Mason)

Mason 2 released in 02/2011

Complete Moosified rewrite
Mason is a viewtroller
Kind of true for Mason 1

But that was 10 years ago

Mason 2 is a pure templating system

The controller bit has been moved to Poet
Mason is too complex
Augmentation
Inheritance
Composition
Method modifiers
Dynamic filters
Mason is too complex
Augmentation
Inheritance
Composition
Method modifiers
Dynamic filters

But wait..
We are programmers
   So it should be just fine
Mason is slow
Hum, well...

Compared to TT (as complex as TT can take
benchmark)

      Rate Mason TT
Mason 909/s -- -53%
TT   1923/s 112% --
Mason is slow - but
Power comes with a price tag

Experience shows it scales very well with
complexity
Mason is slow - but
Power comes with a price tag

Experience shows it scales very well with
complexity

The benchmark I wrote is not bigfat.
Have a look:

https://bitbucket.org/jeteve/mason-pres/
Mason is slow - but honestly




Do we use Perl because it's faster than X?
Mason uses embedded Perl
Prefer PHP?
Mason uses embedded Perl
Or worse, a mini-language ?
Mason uses embedded Perl
Perl is just fine no?

use <Anything you like>;
The basics - Embedded code
% while( my $product = $products->next() ){
<p>Buy our great
    <% $product->name() | H %>.
    It's only <% $product->price() | H %>
 </p>
%}

With DefaultFilter set to H:
<% $product->name() %>
And now, some cool Mason stuff
Augmentation - A typical page



               Header

            Main content

               Footer
Augmentation - Non Mason-ish
/index.mc
<& /comp/header.mi &>
Welcome to index!
<& /comp/footer.mi &>
Augmentation - The Mason Base.mc
/Base.mc
<%augment wrap>
 <html><head><title>Site</title></head>
 <body>
  <% inner() %>
 <body>
</html>
</%augment>
Augmentation - a page
/index.mc
<h1>Welcome</h1>
Augmentation - a page
Evaluates to

<html><head><title>Site</title></head>
 <body>
  <h1>Welcome</h1>
 <body>
</html>
Augmentation - another page?
/products.mc
<ul>
 <li>Lathe</li>
 <li>Piano</li>
</ul>
Augmentation - another page?
Evaluates to ..


Well you get the idea
Augmentation - Become specific
/products/Base.mc
<%augment wrap>
<div class="product">
  <% inner() %>
</div>
</%product>
Augmentation - Become specific
/products/lathe.mc
<h1>Lathe</h1>
Augmentation - Become specific
Evaluates to
<html>
 <head><title>Site</title></head>
 <body>
  <div class="product">
    <h1>Lathe</h1>
  </div>
 <body>
</html>
Augmentation - Wanna go bare?
/products/baremetal.mc
<%flags>
 extends => undef
</%flags>
<h1>Some bare content</h1>

Evaluates to
<h1>Some bare content</h1>
A typical layout
/index

                   Header

                   Menu

               Content

                   Footer
A typical layout
/products/lathe.html
                       Header

                       Menu

                 Share Box
              Product description

                       Footer
A typical layout
/errors/404.html
                   Header

                    Menu

               Error description

                    Footer
Augmentation - Layout control
Typical hierarchy
/layout/Base.mc
/layout/withmenu.mc
/Base.mc
/index.mc
/products/Base.mc
/products/lathe.mc
/errors/Base.mc
/errors/404.mc
Augmentation - Layout control
/layout/Base.mc
<%flags>
  extends => undef
</%flags>
<%augment wrap>
<html>
 <head><title>My site</title></head>
 <body><% inner() %></body>
</html></%augment>
Augmentation - Layout control
/layout/withmenu.mc
<%flags>
  extends => undef
</%flags>
<%augment wrap>
 <div class="menu">...</div>
 <% inner() %>
</%augment>
Augmentation - Layout control
/Base.mc
<%flags>
  extends => '/layout/withmenu.mc'
</%flags>
Augmentation - Layout control
/index.mc
<h1>This is index</h1>
Augmentation - Layout control
/products/Base.mc
<%augment wrap>
 <div class="share">...</div>
 <div class="product">
 <% inner() %>
 </div>
</%augment>
Augmentation - Layout control
/products/lathe.mc
<h1>This is a lathe</h1>
Augmentation - Layout control
/errors/Base.mc
<%flags>
extends => '/layout/Base.mc'
</%flags>
<%augment wrap>
<div class="error"><% inner() %></div>
</%augment>
Augmentation - Layout control
/errors/404.mc
<h1>Sorry, nothing here</h1>
Actually, I want the menu in the error
pages
No problem:
/errors/Base.mc
<%flags>
extends => '/layout/Base.mc'
</%flags>
<%augment wrap>
<div class="error"><% inner() %></div>
</%augment>
And now, inheritance
   And method modifiers
Let's speak about page titles
Remember /layout/Base.mc ?
<%flags>
 inherit => undef
</%flags>
<%method title>Site</%method>
<%augment wrap>
... <title><% $.title() %></title> ...
</%augment>
Now the index title
<%method title>Welcome to Site!</%method>
<h1>This is index</h1>
The lathe.mc
<%after title> - Lathe</%method>
<h1>This is a Lathe</h1>

Renders as:
<title>Site - Lathe</title>
a_product.mc
<%class>
 has 'product' => ( isa => 'My::Product',
                    required => 1 );
</%class>
<%after title> - <% $.product->title() %>
</%after>
<h1>This is a <% $.product->title() %></h1>
a_product.mc
In Catalyst

$c->stash()->{product} = $product;

a_product.mc instance is built with what's on
the stash
a_product.mc
Trivial unit testing

my $mason = Mason->new();

my $p_page = $mason->run
('/products/a_product', product => $p )->output;

ok_contains($p_page , $p->name() );
Composition
 And a bit more
Classic composition
/comp/share.mi
<%class>
  has 'stuff' => ( does => 'Sharable' );
</%class>
<div class="share">
Share <% $.stuff->share_name() %> on blabla
</div>
Composition - New /products/Base.
mc
/products/Base.mc
<%class>
 has 'product' => ( isa => 'Product' );
<%augment wrap>
 <& /comp/share.mi , stuff => $.product &>
 <div class="product">
 <% inner() %>
 </div>
</%augment>
Internal components are
components
So inheritance works

/comp/share/advanced.mi
<%flags>
 extends => '/comp/share.mi'
</%flags>
Advanced share <% $.stuff->share_name() %>
Internal components - Unit testing
my $m = $mason->_make_request();
my $m_text = $m->scomp('/comp/share.mi',
                        p => 'Banana' );

ok_contains($m_text, "Share Banana");
Filters
More powerful than it sounds
A Simple filter
% $.NoBlankLines {{
Hello

World
% }}
Renders:
Hello
World
With argument(s)
% my $i = 1;
% $.Repeat(3) {{
<% $i++ %> 
% }}

Renders:
123
With parametric content
% my $nums = [ 1 , 2 , 3 ];
% $.Loop($nums) {{
% my ( $num ) = @_;
<% $num %>, 
% }}

Renders:
1, 2, 3,
Filters - Make your own
package My::Mason::Filters;
use Mason::PluginRole;
method Iterate($on){
Mason::DynamicFilter->new(
  filter => sub{ my ($yield) = @_;my $txt = '';
      while( my $next = $on->next() ){
         $txt .= $yield->($next); }
      return $txt;
   });}
Filters - Make your own
/Base.mc
<%class>
with My::Mason::Filter;
</%class>
/anywhere
% $.Iterate($resultset){{
% my ($result) = @_;
 This is result <% $result->name() %>!
%}}
Filters - As component
/comp/pager.mi
<%class>
  has 'on' => ( isa => 'DBIx::Class::Resulset' );
  has 'page' => ( isa => 'Int' , default => 1 );
  has 'yield';
</%class>
% my $rs=$.on->search({} , {page => $.page});
<div class="pager">
.. Do stuff with $rs->page() ..</div>
Filters - As component
/comp/pager.mi .. Continued

%# Some paging was output
% $.Iterate($rs){{
% my $stuff = $_[0];
<% $.yield->($stuff) %>
% }}
%# Some other paging maybe?
Let's use our pager
/products/index.mc
<$class>
 has 'products' => ( isa => 'DBIx::Resultset' );
</$class>
% $.CompCall('/comp/page.mi',on=>$.products)
{{
% my ( $product ) = @_;
 Product <% product->name() %>
% }}
Filters can be curried
<%class>
 has 'lang';
 has 'Translate' ( lazy_build => 1 );
 sub _build_Translate{
   my ($self) = @_;
   $self->TranslateIn($self->lang));}
</%class>
% $.Translate(){{
Some Text
% }}
Filters - One more nice one
% $.Cache($key , '10 minute') {{
 <div class="sluggish">
   ....
 </div>
% }}

Combine with currying for cleaner code :)
Conclusions
In case you're not convinced yet
Mason helps writing clean code
Strict by default

Enforce type consistency a-la Moose

Enforce stash content (With Catalyst)

Minimal global variables

Easy unit testing
Mason is powerful
Code the view like a programmer with
inheritance, augmentation, method modifiers,
dynamic filters, currying ..

No change of mindset required

Changes are simple and consistent

Writing a new pages is trivial
Mason is extensible
Base class injection.

my $mason = Mason->new(base_*_class =>
'MyApp::Mason::*Subclass' );

Or Role based Plugins (and Plugins Bundles)

For instance: Mason::Plugin::Cache
Reference
In order of preference
● MasonHQ: http://www.masonhq.com/
● Cpan: Mason
● Cpan: Catalyst::View::Mason2
● Mailing list
● Jerome :P
Give it a go
$ sudo apt-get install libmason-perl
$ echo "Hello world" > mason/index.mc
$ mason.pl mason/index.mc

$ ## In catalyst
$ sudo cpan -i Catalyst::View::Mason2
Questions?
Thanks for your watching!

More Related Content

What's hot

Curso Symfony - Clase 3
Curso Symfony - Clase 3Curso Symfony - Clase 3
Curso Symfony - Clase 3Javier Eguiluz
 
WordPress as a Content Management System
WordPress as a Content Management SystemWordPress as a Content Management System
WordPress as a Content Management SystemValent Mustamin
 
Advanced Custom Fields - Beyond the Basics
Advanced Custom Fields - Beyond the BasicsAdvanced Custom Fields - Beyond the Basics
Advanced Custom Fields - Beyond the BasicsMerrill Mayer
 
CSS Layout Tutorial
CSS Layout TutorialCSS Layout Tutorial
CSS Layout Tutorialhstryk
 
Getting started with woo commerce
Getting started with woo commerceGetting started with woo commerce
Getting started with woo commerceMerrill Mayer
 
Designing for magento
Designing for magentoDesigning for magento
Designing for magentohainutemicute
 
SugarCon 2010 - Best Practices for Creating Custom Apps in Sugar
SugarCon 2010 - Best Practices for Creating Custom Apps in SugarSugarCon 2010 - Best Practices for Creating Custom Apps in Sugar
SugarCon 2010 - Best Practices for Creating Custom Apps in SugarJohn Mertic
 

What's hot (11)

Curso Symfony - Clase 3
Curso Symfony - Clase 3Curso Symfony - Clase 3
Curso Symfony - Clase 3
 
Theme customization
Theme customizationTheme customization
Theme customization
 
DRUPAL Menu System
DRUPAL Menu SystemDRUPAL Menu System
DRUPAL Menu System
 
WordPress as a Content Management System
WordPress as a Content Management SystemWordPress as a Content Management System
WordPress as a Content Management System
 
Advanced Custom Fields - Beyond the Basics
Advanced Custom Fields - Beyond the BasicsAdvanced Custom Fields - Beyond the Basics
Advanced Custom Fields - Beyond the Basics
 
CSS Layout Tutorial
CSS Layout TutorialCSS Layout Tutorial
CSS Layout Tutorial
 
Zend Lab
Zend LabZend Lab
Zend Lab
 
Getting started with woo commerce
Getting started with woo commerceGetting started with woo commerce
Getting started with woo commerce
 
HTML Basics
HTML BasicsHTML Basics
HTML Basics
 
Designing for magento
Designing for magentoDesigning for magento
Designing for magento
 
SugarCon 2010 - Best Practices for Creating Custom Apps in Sugar
SugarCon 2010 - Best Practices for Creating Custom Apps in SugarSugarCon 2010 - Best Practices for Creating Custom Apps in Sugar
SugarCon 2010 - Best Practices for Creating Custom Apps in Sugar
 

Viewers also liked

PerlApp2Postgresql (2)
PerlApp2Postgresql (2)PerlApp2Postgresql (2)
PerlApp2Postgresql (2)Jerome Eteve
 
Except UnicodeError: battling Unicode demons in Python
Except UnicodeError: battling Unicode demons in PythonExcept UnicodeError: battling Unicode demons in Python
Except UnicodeError: battling Unicode demons in PythonAram Dulyan
 
Audience reasearch
Audience reasearchAudience reasearch
Audience reasearchbronnie93
 
Audience reaserch
Audience reaserchAudience reaserch
Audience reaserchFitzzzyyyy
 
Audience reaserch
Audience reaserchAudience reaserch
Audience reaserchChalieHowe
 
Understand unicode & utf8 in perl (2)
Understand unicode & utf8 in perl (2)Understand unicode & utf8 in perl (2)
Understand unicode & utf8 in perl (2)Jerome Eteve
 
Active and Passive audience theories
Active and Passive audience theoriesActive and Passive audience theories
Active and Passive audience theoriesCrispySharp
 
Primary vs. secondary research ig
Primary vs. secondary research igPrimary vs. secondary research ig
Primary vs. secondary research igGeorge Panther
 
Maslow's hierarchy of needs
Maslow's hierarchy of needsMaslow's hierarchy of needs
Maslow's hierarchy of needsPrathamesh Parab
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting PersonalKirsty Hulse
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldabaux singapore
 

Viewers also liked (14)

PerlApp2Postgresql (2)
PerlApp2Postgresql (2)PerlApp2Postgresql (2)
PerlApp2Postgresql (2)
 
Except UnicodeError: battling Unicode demons in Python
Except UnicodeError: battling Unicode demons in PythonExcept UnicodeError: battling Unicode demons in Python
Except UnicodeError: battling Unicode demons in Python
 
Audience reaserch
Audience reaserchAudience reaserch
Audience reaserch
 
Audience reasearch
Audience reasearchAudience reasearch
Audience reasearch
 
Audience reaserch
Audience reaserchAudience reaserch
Audience reaserch
 
Audience reaserch
Audience reaserchAudience reaserch
Audience reaserch
 
The dark knight
The dark knightThe dark knight
The dark knight
 
Understand unicode & utf8 in perl (2)
Understand unicode & utf8 in perl (2)Understand unicode & utf8 in perl (2)
Understand unicode & utf8 in perl (2)
 
Active and Passive audience theories
Active and Passive audience theoriesActive and Passive audience theories
Active and Passive audience theories
 
Primary vs. secondary research ig
Primary vs. secondary research igPrimary vs. secondary research ig
Primary vs. secondary research ig
 
Maslow's hierarchy of needs
Maslow's hierarchy of needsMaslow's hierarchy of needs
Maslow's hierarchy of needs
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 

Similar to Mason - A Template system for us Perl programmers

Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckAnthony Montalbano
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Joao Lucas Santana
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Engine Yard
 
How to Create A Magento Adminhtml Controller in Magento Extension
How to Create A Magento Adminhtml Controller in Magento ExtensionHow to Create A Magento Adminhtml Controller in Magento Extension
How to Create A Magento Adminhtml Controller in Magento ExtensionHendy Irawan
 
Html 5, a gentle introduction
Html 5, a gentle introductionHtml 5, a gentle introduction
Html 5, a gentle introductionDiego Scataglini
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersJames Gray
 
Html5, a gentle introduction
Html5, a gentle introduction Html5, a gentle introduction
Html5, a gentle introduction Diego Scataglini
 
HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2James Pearce
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress WebsitesKyle Cearley
 
Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101Ted Kulp
 
Resource and view
Resource and viewResource and view
Resource and viewPapp Laszlo
 
May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014Chad Windnagle
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practicesmarkparolisi
 
Django Templates
Django TemplatesDjango Templates
Django TemplatesWilly Liu
 
How to Develop a Basic Magento Extension Tutorial
How to Develop a Basic Magento Extension TutorialHow to Develop a Basic Magento Extension Tutorial
How to Develop a Basic Magento Extension TutorialHendy Irawan
 
Twitter bootstrap on rails
Twitter bootstrap on railsTwitter bootstrap on rails
Twitter bootstrap on railsMasakuni Kato
 
Implement rich snippets in your webshop
Implement rich snippets in your webshopImplement rich snippets in your webshop
Implement rich snippets in your webshopArjen Miedema
 

Similar to Mason - A Template system for us Perl programmers (20)

Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages Suck
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
WordCamp Praga 2015
WordCamp Praga 2015WordCamp Praga 2015
WordCamp Praga 2015
 
How to Create A Magento Adminhtml Controller in Magento Extension
How to Create A Magento Adminhtml Controller in Magento ExtensionHow to Create A Magento Adminhtml Controller in Magento Extension
How to Create A Magento Adminhtml Controller in Magento Extension
 
Html 5, a gentle introduction
Html 5, a gentle introductionHtml 5, a gentle introduction
Html 5, a gentle introduction
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and Controllers
 
Html5, a gentle introduction
Html5, a gentle introduction Html5, a gentle introduction
Html5, a gentle introduction
 
HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101
 
Catalog display
Catalog displayCatalog display
Catalog display
 
Resource and view
Resource and viewResource and view
Resource and view
 
May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
 
Django Templates
Django TemplatesDjango Templates
Django Templates
 
How to Develop a Basic Magento Extension Tutorial
How to Develop a Basic Magento Extension TutorialHow to Develop a Basic Magento Extension Tutorial
How to Develop a Basic Magento Extension Tutorial
 
Twitter bootstrap on rails
Twitter bootstrap on railsTwitter bootstrap on rails
Twitter bootstrap on rails
 
Implement rich snippets in your webshop
Implement rich snippets in your webshopImplement rich snippets in your webshop
Implement rich snippets in your webshop
 
Django crush course
Django crush course Django crush course
Django crush course
 

Recently uploaded

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 

Mason - A Template system for us Perl programmers

  • 1. Mason A Template System for us programmers March 2013 - http://www.eteve.net
  • 2. A bit of myth busting ● Mason is old ● Mason is a viewcontroller ● Mason is too complex ● Mason is slow ● Mason uses embedded Perl
  • 3. Mason is old Mason 1 is old (HTML::Mason) Mason 2 released in 02/2011 Complete Moosified rewrite
  • 4. Mason is a viewtroller Kind of true for Mason 1 But that was 10 years ago Mason 2 is a pure templating system The controller bit has been moved to Poet
  • 5. Mason is too complex Augmentation Inheritance Composition Method modifiers Dynamic filters
  • 6. Mason is too complex Augmentation Inheritance Composition Method modifiers Dynamic filters But wait..
  • 7. We are programmers So it should be just fine
  • 8. Mason is slow Hum, well... Compared to TT (as complex as TT can take benchmark) Rate Mason TT Mason 909/s -- -53% TT 1923/s 112% --
  • 9. Mason is slow - but Power comes with a price tag Experience shows it scales very well with complexity
  • 10. Mason is slow - but Power comes with a price tag Experience shows it scales very well with complexity The benchmark I wrote is not bigfat. Have a look: https://bitbucket.org/jeteve/mason-pres/
  • 11. Mason is slow - but honestly Do we use Perl because it's faster than X?
  • 12. Mason uses embedded Perl Prefer PHP?
  • 13. Mason uses embedded Perl Or worse, a mini-language ?
  • 14. Mason uses embedded Perl Perl is just fine no? use <Anything you like>;
  • 15. The basics - Embedded code % while( my $product = $products->next() ){ <p>Buy our great <% $product->name() | H %>. It's only <% $product->price() | H %> </p> %} With DefaultFilter set to H: <% $product->name() %>
  • 16. And now, some cool Mason stuff
  • 17. Augmentation - A typical page Header Main content Footer
  • 18. Augmentation - Non Mason-ish /index.mc <& /comp/header.mi &> Welcome to index! <& /comp/footer.mi &>
  • 19. Augmentation - The Mason Base.mc /Base.mc <%augment wrap> <html><head><title>Site</title></head> <body> <% inner() %> <body> </html> </%augment>
  • 20. Augmentation - a page /index.mc <h1>Welcome</h1>
  • 21. Augmentation - a page Evaluates to <html><head><title>Site</title></head> <body> <h1>Welcome</h1> <body> </html>
  • 22. Augmentation - another page? /products.mc <ul> <li>Lathe</li> <li>Piano</li> </ul>
  • 23. Augmentation - another page? Evaluates to .. Well you get the idea
  • 24. Augmentation - Become specific /products/Base.mc <%augment wrap> <div class="product"> <% inner() %> </div> </%product>
  • 25. Augmentation - Become specific /products/lathe.mc <h1>Lathe</h1>
  • 26. Augmentation - Become specific Evaluates to <html> <head><title>Site</title></head> <body> <div class="product"> <h1>Lathe</h1> </div> <body> </html>
  • 27. Augmentation - Wanna go bare? /products/baremetal.mc <%flags> extends => undef </%flags> <h1>Some bare content</h1> Evaluates to <h1>Some bare content</h1>
  • 28. A typical layout /index Header Menu Content Footer
  • 29. A typical layout /products/lathe.html Header Menu Share Box Product description Footer
  • 30. A typical layout /errors/404.html Header Menu Error description Footer
  • 31. Augmentation - Layout control Typical hierarchy /layout/Base.mc /layout/withmenu.mc /Base.mc /index.mc /products/Base.mc /products/lathe.mc /errors/Base.mc /errors/404.mc
  • 32. Augmentation - Layout control /layout/Base.mc <%flags> extends => undef </%flags> <%augment wrap> <html> <head><title>My site</title></head> <body><% inner() %></body> </html></%augment>
  • 33. Augmentation - Layout control /layout/withmenu.mc <%flags> extends => undef </%flags> <%augment wrap> <div class="menu">...</div> <% inner() %> </%augment>
  • 34. Augmentation - Layout control /Base.mc <%flags> extends => '/layout/withmenu.mc' </%flags>
  • 35. Augmentation - Layout control /index.mc <h1>This is index</h1>
  • 36. Augmentation - Layout control /products/Base.mc <%augment wrap> <div class="share">...</div> <div class="product"> <% inner() %> </div> </%augment>
  • 37. Augmentation - Layout control /products/lathe.mc <h1>This is a lathe</h1>
  • 38. Augmentation - Layout control /errors/Base.mc <%flags> extends => '/layout/Base.mc' </%flags> <%augment wrap> <div class="error"><% inner() %></div> </%augment>
  • 39. Augmentation - Layout control /errors/404.mc <h1>Sorry, nothing here</h1>
  • 40. Actually, I want the menu in the error pages No problem: /errors/Base.mc <%flags> extends => '/layout/Base.mc' </%flags> <%augment wrap> <div class="error"><% inner() %></div> </%augment>
  • 41. And now, inheritance And method modifiers
  • 42. Let's speak about page titles Remember /layout/Base.mc ? <%flags> inherit => undef </%flags> <%method title>Site</%method> <%augment wrap> ... <title><% $.title() %></title> ... </%augment>
  • 43. Now the index title <%method title>Welcome to Site!</%method> <h1>This is index</h1>
  • 44. The lathe.mc <%after title> - Lathe</%method> <h1>This is a Lathe</h1> Renders as: <title>Site - Lathe</title>
  • 45. a_product.mc <%class> has 'product' => ( isa => 'My::Product', required => 1 ); </%class> <%after title> - <% $.product->title() %> </%after> <h1>This is a <% $.product->title() %></h1>
  • 46. a_product.mc In Catalyst $c->stash()->{product} = $product; a_product.mc instance is built with what's on the stash
  • 47. a_product.mc Trivial unit testing my $mason = Mason->new(); my $p_page = $mason->run ('/products/a_product', product => $p )->output; ok_contains($p_page , $p->name() );
  • 48. Composition And a bit more
  • 49. Classic composition /comp/share.mi <%class> has 'stuff' => ( does => 'Sharable' ); </%class> <div class="share"> Share <% $.stuff->share_name() %> on blabla </div>
  • 50. Composition - New /products/Base. mc /products/Base.mc <%class> has 'product' => ( isa => 'Product' ); <%augment wrap> <& /comp/share.mi , stuff => $.product &> <div class="product"> <% inner() %> </div> </%augment>
  • 51. Internal components are components So inheritance works /comp/share/advanced.mi <%flags> extends => '/comp/share.mi' </%flags> Advanced share <% $.stuff->share_name() %>
  • 52. Internal components - Unit testing my $m = $mason->_make_request(); my $m_text = $m->scomp('/comp/share.mi', p => 'Banana' ); ok_contains($m_text, "Share Banana");
  • 54. A Simple filter % $.NoBlankLines {{ Hello World % }} Renders: Hello World
  • 55. With argument(s) % my $i = 1; % $.Repeat(3) {{ <% $i++ %> % }} Renders: 123
  • 56. With parametric content % my $nums = [ 1 , 2 , 3 ]; % $.Loop($nums) {{ % my ( $num ) = @_; <% $num %>, % }} Renders: 1, 2, 3,
  • 57. Filters - Make your own package My::Mason::Filters; use Mason::PluginRole; method Iterate($on){ Mason::DynamicFilter->new( filter => sub{ my ($yield) = @_;my $txt = ''; while( my $next = $on->next() ){ $txt .= $yield->($next); } return $txt; });}
  • 58. Filters - Make your own /Base.mc <%class> with My::Mason::Filter; </%class> /anywhere % $.Iterate($resultset){{ % my ($result) = @_; This is result <% $result->name() %>! %}}
  • 59. Filters - As component /comp/pager.mi <%class> has 'on' => ( isa => 'DBIx::Class::Resulset' ); has 'page' => ( isa => 'Int' , default => 1 ); has 'yield'; </%class> % my $rs=$.on->search({} , {page => $.page}); <div class="pager"> .. Do stuff with $rs->page() ..</div>
  • 60. Filters - As component /comp/pager.mi .. Continued %# Some paging was output % $.Iterate($rs){{ % my $stuff = $_[0]; <% $.yield->($stuff) %> % }} %# Some other paging maybe?
  • 61. Let's use our pager /products/index.mc <$class> has 'products' => ( isa => 'DBIx::Resultset' ); </$class> % $.CompCall('/comp/page.mi',on=>$.products) {{ % my ( $product ) = @_; Product <% product->name() %> % }}
  • 62. Filters can be curried <%class> has 'lang'; has 'Translate' ( lazy_build => 1 ); sub _build_Translate{ my ($self) = @_; $self->TranslateIn($self->lang));} </%class> % $.Translate(){{ Some Text % }}
  • 63. Filters - One more nice one % $.Cache($key , '10 minute') {{ <div class="sluggish"> .... </div> % }} Combine with currying for cleaner code :)
  • 64. Conclusions In case you're not convinced yet
  • 65. Mason helps writing clean code Strict by default Enforce type consistency a-la Moose Enforce stash content (With Catalyst) Minimal global variables Easy unit testing
  • 66. Mason is powerful Code the view like a programmer with inheritance, augmentation, method modifiers, dynamic filters, currying .. No change of mindset required Changes are simple and consistent Writing a new pages is trivial
  • 67. Mason is extensible Base class injection. my $mason = Mason->new(base_*_class => 'MyApp::Mason::*Subclass' ); Or Role based Plugins (and Plugins Bundles) For instance: Mason::Plugin::Cache
  • 68. Reference In order of preference ● MasonHQ: http://www.masonhq.com/ ● Cpan: Mason ● Cpan: Catalyst::View::Mason2 ● Mailing list ● Jerome :P
  • 69. Give it a go $ sudo apt-get install libmason-perl $ echo "Hello world" > mason/index.mc $ mason.pl mason/index.mc $ ## In catalyst $ sudo cpan -i Catalyst::View::Mason2