Loading ...
Sorry, an error occurred while loading the content.

239Re: Templating suggestions?

Expand Messages
  • eamondaly
    Mar 22, 2002
      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

      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'});

      sub my::header {
      $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" />

      Welcome back, <%= $Session->{'username'} %>!

      <!--#include file="foo.inc" -->

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

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

      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
      > 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

      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

      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: asp-unsubscribe@...
      For additional commands, e-mail: asp-help@...
    • Show all 6 messages in this topic