.HT Files - Make HTML Templates Easier To Use & Maintain

Internet / Web related innovations
  • Ads

.HT Files - Make HTML Templates Easier To Use & Maintain

Postby admin » Sat Oct 17, 2009 4:21 am

Hi, all, to kick the Innovation Forum off, here's an FYI about a new tool (Creative Commons licensed).

I've used HTML/CSS templates for sites for years, and love them, BUT have been frustrated at the redundant effort involved in setting up and maintaining the pages. Changing the navigation when it's embedded in every page, or adding an ad banner, so many little changes require editing every file. Not my cup of tea... so I wrote a tool to keep web site template themes apart from the content.

I've used this for years now and love it. Saved me hundreds if not thousands of hours. (Not to mention much easier on the whole than trying to merge custom code with Drupal/etc.)

What I do is use ".HT" files. .HT is a file type that combines the template at run time, like a CMS would do, but without any hooks inside the content pages. None. They're 100% unique page content. It 's sort of like how a .css file moves styles to one place -- but this does it for the template's HTML.

This link--

http://tech-soft.com/ht

-- details what I did and how you can do it too. It's released under a Creative Commons license. Hope folks find it useful.

-- Dr. Andrew Burt
admin
Name: Andrew Burt
Site Admin
 
Posts: 37
Joined: Fri Oct 16, 2009 11:48 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby seliza » Sun Oct 25, 2009 4:56 pm

Three days ago, I was updating my website. I'm a dabbler and not a serious designer, and I have no fancy programs because I'm additionally a poor starving college student, so my website's fairly simple CSS and HTML. I was going through, page by page, adding in a link to my navigation menu, and thinking to myself, "Is there some easier way to do this that I don't know?" This just sounds like that easier way, so I'm going to have to study through it and give it a try. Thanks!
seliza
Sprout
 
Posts: 1
Joined: Sun Oct 25, 2009 4:44 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby admin » Sat Oct 31, 2009 4:11 pm

Great, let me know how it turns out.
admin
Name: Andrew Burt
Site Admin
 
Posts: 37
Joined: Fri Oct 16, 2009 11:48 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby MikeAlx » Tue Nov 03, 2009 7:31 pm

Thanks Andrew - that's interesting. I've not played with handlers - not sure if I have access to httpd.conf (my sites are remotely hosted). Can you achieve the same thing in .htaccess files?

I tend to call the surrounding stuff "wrappers" or "jackets". The technique I use in php is to start buffering output, then at the end of each page (ie the main content), grab the buffer contents into a variable, end buffering, then include the wrapper page (which also outputs the main content from the variable). It's a bit long-winded but I find it fairly clean.

This is a rough example:
Code: Select all
main page file:

<?php
  ob_start(); // start buffering
?>
-- main page content start --
  blah
  blah
  blah
-- end of main content --
<?php
  $page_content = ob_get_contents();  // get buffer contents
  ob_end_clean();  // clear and close buffer
  include("/pathto/jacket.php");  // include wrapper page
?>


wrapper file "jacket.php":

-- header blah --
-- left bar blah --
<?php echo($page_content); ?>
-- right bar blah --
-- footer blah --


I guess an alternative to a filetype handler is to use mod_rewrite, but that can be sluggish and tends to get messy in my experience.
MikeAlx
Sprout
 
Posts: 7
Joined: Tue Nov 03, 2009 7:12 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby admin » Wed Nov 04, 2009 10:35 pm

MikeAlx wrote:Thanks Andrew - that's interesting. I've not played with handlers - not sure if I have access to httpd.conf (my sites are remotely hosted). Can you achieve the same thing in .htaccess files?

I tend to call the surrounding stuff "wrappers"


Yes, "wrapper" is a common name but, you know, being a writer with a thesaurus... :)

Yes, you should be able to accomplish the same thing with a .htaccess file. I tested one with this in it--
Code: Select all
AddHandler encapsulated-file-type .ht
Action encapsulated-file-type /cgi-bin/encapsulator.cgi
DirectoryIndex index.html index.cgi index.php index.ht

--and it worked like a charm.

The main problem with code inside each page is that it's... inside each and every page. :) I wanted something where I didn't have to put any hooks whatsoever inside the content page. (It also then allows outside users to create pages that will have the site's theme, but without them having to futz with or mess up code inside the page. A quick approach to my top/bottom file method would be to include("top") at the top and likewise at the bottom, but it's still a hook inside the content page, which I wanted to avoid.)

Anyway -- let me know how the .htaccess approach works for you.
admin
Name: Andrew Burt
Site Admin
 
Posts: 37
Joined: Fri Oct 16, 2009 11:48 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby MikeAlx » Wed Nov 04, 2009 11:45 pm

It's certainly a nice clean solution, which I may well have use for in the future. Just now I'm working on something so heavily database-driven that most pages are full of php anyway! I tend to develop sites with a user log-in, so I'd have to ensure the session start and authentication stuff was handled before any other output (php insists on this; I can't remember if PERL is similarly fussy). I'd probably build this into the encapsulator script, or possibly the header content file.

It occurs to me that, if you wanted a choice of templates, you could have an html comment in the main file telling the encapsulator what template style to use. Slightly less clean, and more margin for messing things up - but at least if the encapsulator can't make sense of it, it can fall back on a default style; whereas if someone messes up hook-in script, the consequences could be a lot worse!
MikeAlx
Sprout
 
Posts: 7
Joined: Tue Nov 03, 2009 7:12 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby MikeAlx » Thu Nov 05, 2009 3:12 pm

Further to the above thought, I've been playing with your script (works very nicely from .htaccess, as you say) and done a little code to implement custom templates. I appended a function that extracts the template name from an HTML comment in the main file, thus:
Code: Select all
# gets the specified template name in the file
sub getTemplateName {
    my $string = do { local( @ARGV, $/ ) = $ENV{PATH_TRANSLATED} ; <> } ;  # slurp file
    my $tplname="";
    @split = split(/<!-- TPL=/, $string, 2);    # get TPL= comment start
    if (@split>1){
        @split2 = split(/ -->/, $split[1], 2);  # extract template name
        $tplname = trim($split2[0]);
    }
    return $tplname;
}

# trims white space from a string
sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}


and modified the output code thus:

Code: Select all
$template = getTemplateName();
$head = ($template eq "")? "" : "$dir/$template"."_head.htm";
$foot = ($template eq "")? "" : "$dir/$template"."_foot.htm";

if (($head ne "") && (-r "$head")){
  cat($head);
}
elsif (-r "$dir/top") {
   cat("$dir/top");
}
else {
   cat("$root/top");
}

cat($ENV{PATH_TRANSLATED});

if (($foot ne "") && (-r "$foot")){
  cat($foot);
}
elsif (-r "$dir/bottom") {
   cat("$dir/bottom");
}
else {
   cat("$root/bottom");
}


This works a treat. If you want a different header or footer template, just put somewhere in the main file:
Code: Select all
<!-- TPL=customtemplate -->

and create header and footer templates called customtemplate_head.htm and/or customtemplate_foot.htm.

Of course, slurping the file to get the template info will slow things down a little, so it's best left out if not needed. Also, I've used the split function to get at the template name; regular expressions might be more efficient (but I tend to get in a muddle with them!).
MikeAlx
Sprout
 
Posts: 7
Joined: Tue Nov 03, 2009 7:12 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby admin » Thu Nov 05, 2009 4:57 pm

Very cool mod. I confess I've modded mine for some custom features too, but I didn't think most people would be interested. :) For example, I put in a conditional feature I find useful, namely <!--#debug:...html...-->, where if the IP# is from my PC then it sends through the HTML (which presumably includes debug info I want to see but regular visitors wouldn't/shouldn't). I also put in a hook for multiple templates, and an #include facility that is the same syntax as .shtml. (I use that on Critters site to #include the Notices at the top of the most visited pages.) I've also thought about a feature for handling themes that have multiple content areas, i.e. a top, bottom and a "middle" HTML (for two-column layouts where there's unique content in both columns).

I would think adding session control would be pretty easy inside the encapsulator, as you say. Yes, it would need to be before the first HTML in Perl too, since it involves HTTP headers for cookies.

Glad you got it working for you and are having fun with it!
admin
Name: Andrew Burt
Site Admin
 
Posts: 37
Joined: Fri Oct 16, 2009 11:48 pm

Re: .HT Files - Make HTML Templates Easier To Use & Maintain

Postby admin » Thu Nov 05, 2009 5:16 pm

MikeAlx wrote:Of course, slurping the file to get the template info will slow things down a little, so it's best left out if not needed. Also, I've used the split function to get at the template name; regular expressions might be more efficient (but I tend to get in a muddle with them!).


I love RE's but I agree they're pretty dense. To get the template name with RE's you could do something like
Code: Select all
$htmlstring =~ /<!--\s*TPL="*(\w+)/;
$tplname = $1;

The first line does a regexp match against the whole HTML blob. The pattern parses as...

<!-- Your comment lead in
\s* skip any white space found (\s for "whitespace", "*" for "zero or more")
TPL= syntax for start of template name
"* skip any quote character (could do ['"]* to skip both ' and " quotes if desired)
( remember the following stuff as $1
\w+ any one or more alphanumeric characters (\w = any alnum, '+' for "one or more")
) end of "remembered" $1 pattern

Could also replace \w+ with [\w.-]+ to allow '.' and '-' characters in the template name. But note that '-' must be the last character inside [] or else it specifies a range of characters; at the end of the list it means '-' itself.

Also, to avoid the double file slurp, you could replace the call to cat() for the content with a two-step process -- read the file into a string before dumping the "top" file; parse for TPL= or whatnot; cat the top file; print the already slurped HTML string; cat the bottom file.

Have fun!
admin
Name: Andrew Burt
Site Admin
 
Posts: 37
Joined: Fri Oct 16, 2009 11:48 pm

  • Ads

Return to Internet / Web

cron