Reply to topic
HTML templating
spandox


Joined: 03 Jul 2004
Posts: 17
Location: Delaware
Reply with quote
I have been asked a couple of times how to have perl just do some search and replaces in an html document. So that you can make your html in dreamweaver or something and put in a code for the "dynamic" part.

Example template:
Code:
<HTML><HEAD><TITLE>[title]</TITLE></HEAD>
<BODY>
<H1>[title]</H1>
This is a test of the [system] system.
[test]
</BODY>
</HTML>


I know it is not complex - But what the hey - it is an example

I wanted to be able to replace [things] with other bits - generated from code.
So lets say I make a hash:
Code:
my %moo = ('system'=>'Groovy', 'title'=>'Groovy Title');


Why not be able to automatically generate the html:

Code:
<HTML><HEAD><TITLE>Groovy Title</TITLE></HEAD>
<BODY>
<H1>Groovy Title</H1>
This is a test of the Groovy system.
[test]
</BODY>
</HTML>


Here is a module I wrote to help with the process:

Code:
package mhsTemplate;
use strict;
require 5.000;


require Exporter;

@mhsTemplate::ISA = qw( Exporter );
@mhsTemplate::EXPORT = qw(&getSources &doTemplate &doTemplateND);

sub doTemplate ($$@)
   # IN: filename, "default", a hash of replacement values
   # default is used to replace keys that are not given in
   # the hash
   # OUT: a document where the replacements have been done
{
   my $in = shift;
   my $default = shift;
   my %vals = @_;
   my $out = "";
   open(IN, '<', $in) or die "Can't open file $in $!\n";
   while (<IN>)
   {
      s/\[(.*?)\]/((exists $vals{$1})?$vals{$1}:$default)/ge;
      $out .= $_;
   }
   close IN;
   return $out;
}

sub doTemplateND ($@)
   # IN: filename, a hash of replacement values
   # keys not in hash are left
   # OUT: a document where the replacements have been done
{
   my $in = shift;
   my %vals = @_;
   my $out = "";   open(IN, '<', $in) or die "Can't open file $in $!\n";
   while (<IN>)
   {
      s/\[(.*?)\]/((exists $vals{$1})?$vals{$1}:"[$1]")/ge;
      $out .= $_;
   }
   close IN;
   return $out;
}


sub getSources ($)
{
   # IN: filename
   # OUT: keys for the template
   my $in = shift;
   my %names;
   open(IN, '<', $in) or die "Can't open file $in $!\n";
   while (<IN>)
   {
      while (s/\[(.*?)\]/$names{$1}++/e){}
   }
   close IN;
   return keys %names;
}

1;


and an example of it in use
Code:
#!/usr/bin/perl

use mhsTemplate;
my %moo = ('system'=>'Groovy', 'title'=>'Groovy Title');
print doTemplateND("moo.tmpl",%moo);


ND is is for no default - If I wanted to put a default in for when there is no key defined I can use the non-ND one.

If I want a list of keys from a file I can use getSources

Let me know if this is useful to anyone
spandox


Joined: 03 Jul 2004
Posts: 17
Location: Delaware
Reply with quote
Oh - don't forget to print a content-type is using it as a perl script:
Code:
#!/usr/bin/perl
use CGI ':standard';
use mhsTemplate;
print header();
my %moo = ('system'=>'Groovy', 'title'=>'Woof');
print doTemplate("moo.tmpl","default",%moo);
Almost
spandox


Joined: 03 Jul 2004
Posts: 17
Location: Delaware
Reply with quote
Not quite the same. The OBJECT tag creates an iframe withing the document. This is essentially a whole new embedded web browser. Not only CAN you put a complete HTML document in - YOU MUST. Style sheets etc do not propigate. This is not a big deal - just re-link the sytle sheet from within code. The big deal is that neither do the values passed to the called document with a POST or GET from a form.

The object tag is good for a static page that has a small portion of dynamic content that is not interactive - or to do a framed skin around a user interface, but it is quite difficult to use to build an application with it (as in anything other than a border - it is just a frame)

When working with my artists what I will often do is have them make html mock-ups of what they want. I make a template directory. In this directory the all of the static portions of the code get placed. A perl script opens up a template and build the form (login-in screen, or personal preferences or whatever) and spits it out to the web browser.

If the artist ever wants to "reskin" the application they just change the templates. Sometimes the [replaced text] is just a word or two - sometimes it is rows in a table - sometimes it is form elements.

If all you want to do is add a username after the world "Hello" on the top of a website creating a frame is just plain silly. Likewise creating a big application that has many interactive elements that has a contigious look and feel with the site trying to put all of the dynamic portion in iframes (or OBJECT tags) is just not a task I would want to undertake.
HTML templating
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic