Share a variable between components

On my previous posts we saw how to to create and use true global variables. Their usage is limited to $dbh at the moment. But I also need a “global” variable, shared between all components of the same request. Here’s one of my first tries, and this is how the Base.mc will look like:

has 'title';
has 'authenticated_user';

<%augment wrap>
 <html>
 <head>
 <link rel="stylesheet" href="/static/css/style.css">
% $.Defer 
 </head>
 <body>
 <% inner() %>
 </body>
 </html>
<%init>
  $.authenticated_user('Admin');
</%init>
</%augment>

and this is the index.mc:

Welcome, <% $.authenticated_user %>
<%init>
$.title("Welcome to Sentosa");
</%init>

I think I will use Base.mp and here’s what I’m going to do:

has 'authenticated_user';
has 'title';

method wrap() {
 $.authenticated_user ("Superuser");
 inner();
}

Next, how do I actually authenticate my users? See it on my next post!

Written on August 10, 2015