Joshua Chamas, you're my hero.

The key, of course, was the Script_OnStart event. Declaring %var
there made $var{'TITLE'}, $var{'HEADER'}, $var{'BODY'}, etc.
available to the page, its template, and its includes. You're a
life-saver!

For those curious, we ended up with the following:

global.asa contains:

    sub Script_OnStart {
      use vars qw(%var);
      %var = ();
    }

    sub Script_OnEnd {
      $var{'TITLE'} ||= $var{'HEADER'};
      my $template_ref = $Response->TrapInclude($var{'TEMPLATE'});
      $main::Response->Write($$template_ref);
    }
    
    sub my::header {
      shift;
      $var{'HEADER'} .= shift;
    }

    sub my::template {
      my $args = shift;
      $var{'TEMPLATE'} = $Server->MapPath($args->{'href'});
    }

    sub my::body {
      my $args = shift;
      $var{'BODY'} = shift;
    }

A typical page, foo.html, contains:

    <my:template href="/templates/simple.html" />

    <my:header>
    Welcome back, <%= $Session->{'username'} %>!
    </my:header>

    <my:body>
    <!--#include file="foo.inc" -->
    </my:body>

And simple.html contains all the pretty formatting like so:

    <html>
    <head><title><%= $var{'TITLE'} %></title></head>
    <body>
    <h1><%= $var{'HEADER'} %></h1>
    <%= $var{'BODY'} %>
    </body>
    </html>

The really great thing is that code can be added anywhere: to
foo.html, simple.html, to their includes, in the subs-- it's
ridiculously flexible. Plus, it's pretty similar to our existing
module, which means I won't have a tough time retraining my staff.

> I have long wanted to build support for easier variables 
substitution
> where one could do this:
> 
>   PerlSetVar QuickVars 1
> 
> sub Script_OnStart { 
>    $Vars->{DATA} = 1;
> }
> 
> Then in the ASP script, PHP quick variables would be supported like:
> 
> <% for(1..10) { %>
>   $DATA
> <% } %>
> 
> $DATA would be pulled from $Vars->{DATA} automatically at runtime.
> I think this would be a worthwhile extension to Apache::ASP since
> <%= $Vars->{DATA} %> type templates can be unwieldy for HTML 
developers.

That's pretty much exactly how we do it:

  s/\$(\w+)/(exists $var{$1}) ? $var{$1} : "\$$1" /seg;

We also have some special hashes, like %input and %db, which simplify
development:

  s/\$input{'(\w+)'}/(exists $input{$1}) ? $input{$1} : "\$$1" /seg;

Seems like we could just pop that into Script_OnEnd.

Anyway, thanks again, Joshua. This stuff is great.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to