Re: Setting page title and other meta-data in wrapper using Mason 2.0

Jonathan Swartz <[email protected]> Sun, 15 Jul 2012 16:27:52 -0700
Newsgroups gmane.comp.web.mason.user
Message-ID <[email protected]>
Hi Kamil,

Your solution is mostly correct - what you need is the Defer feature:

    https://metacpan.org/module/Mason::Plugin::Defer

This allows you to defer the computation of certain output until the very end of the request.

In your autohandler, just do this:

    <title><% $m->defer(sub { $self->title }) %></title>

or alternatively

    % $.Defer {{
    <% $self->title %>
    % }}

So that $self->title won't be computed until after it has been set in the page.

This feature is powerful but kind of hidden in the docs, I need to remedy this.

Hope that helps!

Jon


On Jul 15, 2012, at 1:53 PM, Kamil Niechajewicz wrote:

> Hello,
> 
> In our upcoming project we've decided to give Mason 2.0 a go - we'll be completly rewritting pretty big web portal with over 10 years of history which had previously been written in old Mason/mod_perl. 
> 
> I checked documentation and I really like the direction of 2.0 version, but considering our needs I have one important question: biggest problem with old Mason was using infromation from top page component (the one which was accessed from browser, like index.m) in upper "wrappers" that decided on things like meta data, HTML header and base layout. 
> 
> To give an example (pardon pseudo code but I just want to make it clear as quick as possible) in Mason 1.x:
> 
> ---------------------------------------- 
> /index.m
> <%init>
>    my $article = Article::get_article(...);
>    my $title = "Article: " . $article->{title};
> </%init>
> <h1><% $article->{title}</h1>
> <% $article->{body} %>
> 
> 
> ---------------------------------------- 
> /autohandler
> <%init>
>   my $title = "Default title";
> </%init>
> <html>
> <head>
> <title><% $title %></title>     // $title should be either default or
>                                 // overriden by base components like index.m
> </head>
> <body>
> <% $m->call_next %>
> 
> In Mason 1.x it was quite hard (if not impossible) to get that $title from base component (index.m) up to autohandler which required that information to set up <title></title> of web page. <%attr> block worked a bit like this, but it was static and could not be changed depending on some logic (like setting title of fetched article etc.)
> 
> My question is - how can this be achieved in Mason 2.0 - I suppose that having components as classes help here a lot? I couldn't find the exact example of such "passing" of attributes. If Base.mc is some kind of autohandler (if I'm correct) then it will become a parent of index.mc component, right? If so, can I define
> 
> /Base.mc
> <%class>
>   has 'title'
> </%class>
> <html>
> <head>
> <title><% $self->title %></title>
> </head>
> <body>
> <% inner() %>
> 
> and then in index.mc do this:
> 
> /index.mc
> <%init>
>   $self->title('New title');
> </%init>
> 
> And it will cause the Base.mc component class to display the title set by its child? Or should we approach this more in a OO manner defining some virtual method in Base.mc like GetPageTitle() and override it in other components with a method returning a specific title? 
> 
> I'd be really grateful for some short examples of good practices of doing this in Mason 2.0 :) 
> 
> Thanks,
> 
> KN
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and 
> threat landscape has changed and how IT managers can respond. Discussions 
> will include endpoint security, mobile security and the latest in malware 
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
> Mason-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mason-users

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Mason-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mason-users