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

232Re: Templating suggestions?

Expand Messages
  • Joshua Chamas
    Mar 21, 2002
    • 0 Attachment
      eamondaly wrote:
      >
      > This is going to be a long post, so please bear with me.
      >
      > I'm implementing a new site in Apache::ASP. I'd like to develop a
      > framework similar to our existing mod_perl setup. The way we
      > currently create a page is like so:
      >
      > - Templater creates template.html
      > - Contains several tokens like __TITLE__, __HEADER__, __BODY__,
      > __LEFT_COLUMN__
      > - Some default variables are set: $var{'LEFT_COLUMN'},
      > $var{'FOOTER'}, etc.
      > - Also contains some perl logic
      >

      If you want some standard templatting system other than
      ASP style <%= $vars{TITLE} %>, then why not use another
      templatting framework like Template Toolkit or Embperl ?
      I would not invent yet another templatting language
      when there are so many to choose from. Please see:

      http://www.perl.com/pub/a/2001/08/21/templating.html

      for templatting comparisons.

      If you want to create an XMLSubs templatting system, you
      can do that within Apache::ASP like

      <var name='TITLE' />

      If you REALLY want to do __TITLE__, you could do this in
      global.asa...

      use vars qw(%vars);
      sub Script_OnStart {
      %vars = (); # init per request
      }

      sub Script_OnFlush {
      my $data = $Response->{BinaryRef};
      $$data =~ s/__(\w+)__/$vars{$1}/isge;
      }

      > - Developer creates foo.html
      > - Sets $var{'TEMPLATE'} to template.html
      > - foo.html (optionally) contains variables like $var{'TITLE'},
      > $var{'HEADER'}, and $var{'LEFT_COLUMN'}. These append to or
      > override those set in the template
      > - Everything else in foo.html is treated as $var{'BODY'}
      > - Also contains perl logic
      >

      I am not sure where you are going with $var{BODY}, but you might
      be more interested in using mod_perl handlers with a templatting
      system instead like Template Toolkit or HTML::Template.

      > When a request comes in for foo.html, our perl module scans it for
      > the template name, reads in and executes everything in template.html,
      > executes everything in foo.html, substitutes all tokens with their
      > values (__TOKEN__ with $var{'TOKEN'}), and spits out the result. Can
      > I mimic this behavior in Apache::ASP?
      >

      Script_OnFlush handler can be used for doing any post processing
      of output your heart desires. But weight carefully the difference
      between can & should in this regards.

      > I've been trying things along the lines of (in foo.html):
      >
      > <my:template href="/templates/simple.inc">
      > WHEE! <% print scalar localtime %>
      > </my:template>
      >
      > and then, in global.asa:
      >
      > sub my::template {
      > my($args, $html) = @_;
      > my $template = $Server->MapPath($args->{'href'});
      > my $template_ref = $Response->TrapInclude($template);
      > $$template_ref =~ s/__BODY__/$html/;
      > $main::Response->Write($$template_ref);
      > }
      >
      > which works, but obviously only gets me halfway there. Can I declare
      > variables like %var in foo.html that are accessible to the routine
      > my::template? I can't set these as globals, because they're not.
      >

      This is a pretty good use of XMLSubs to create your own
      mini template system. If $template is just an HTML template
      and you don't want HTML developers adding ASP bits, then you
      might just read in the file directly with

      if(-e $file) {
      open(FILE, $file) || die("can't open file: $!");
      my $data = join('', <FILE>);
      close FILE;
      }

      This way your templates are safe from ASP code blocks,
      and gives you greater control of the template system.
      In order to allow XMLSubs tags use, but not ASP code
      blocks, I have considered configs or params to
      the Include() calls like CodeBlocks => 0 as in

      PerlSetVar CodeBlocks 0

      or

      $Response->Include({ File => 1, CodeBlocks => 0 }, @args);

      which could still allow for XMLSubs while disabling <% %>
      in a script/template. One of the complaints of ASP is
      that is allows too much to be put into templates, and while
      I see the above would be nice to have, no one has yet NEEDED
      it, thus is has never been done.

      > I really don't want our developers coding pages like:
      >
      > <!--#include file="everything_up_to_the_title.inc">
      > This is my title!
      > <!--#include file="everything_from_title_to_left_column.inc">
      > This is my left column
      > <!--#include file="everything_from_left_column_to_header.inc">
      > ...

      I agree, this is not so good these days, but Apache::ASP supports
      it for SSI backwards compatibility.

      > I'm open to any and all suggestions!

      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.

      I would adopt a standard of $\w+ for regexp matching for QuickVars
      since this is the kind of thing supported in PHP & is therefore
      a widely used practice. As a PerlSetVar QuickVars 1 config, this would
      have the added benefit of not affecting those Apache::ASP users until
      they explicitly want this feature.

      --Josh
      _________________________________________________________________
      Joshua Chamas Chamas Enterprises Inc.
      NodeWorks Founder Huntington Beach, CA USA
      http://www.nodeworks.com 1-714-625-4051

      ---------------------------------------------------------------------
      To unsubscribe, e-mail: asp-unsubscribe@...
      For additional commands, e-mail: asp-help@...
    • Show all 6 messages in this topic