Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Assembling text into websites

by ggvaidya (Pilgrim)
on Oct 23, 2008 at 18:02 UTC ( [id://719103]=perlquestion: print w/replies, xml ) Need Help??

ggvaidya has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks:

I've got to make two small websites, six to ten pages each. Neither of these webservers support any sort of server-side insertion or scripting. I've got a number of common elements (page header, navigation bar, and so on) which need to be identical across all these pages.

I've been looking for a module which will allow me to "assemble" these pages from source files. I only need two bits of functionality: my individual page files should be able to specify templates to be inserted at the appropriate places, and in specifying these templates, I should be able to pass values to them.

(Since I ended up following the suggestions in the comments, the following description of how I planned to do it is irrelevant. I've wrapped it in <readmore>s that it may trouble us mortals no more).

For example, index.txt might contain:

[:top |title=Welcome to | is blah blah blah :] <p>Welcome to [..]</p>

And top.txt will contain:

[:preamble:] <head> <title>[:$title:]</title> </head> <body>

To my surprise, I can't find a simple module which accomplishes this: there's plenty of template engines, but they're designed for on-the-fly generation of page layouts for dynamic websites, and are largely overkill for my task. I strongly suspect that I can't come up with the right search terms, and that such a module must exist on CPAN somewhere.

Does anybody have any ideas on how to find this module, or am I just going about this task all wrong? I've already started rolling-my-own, with the template format mentioned above, so a module to do this will definitely exist on CPAN in a week or two :-). But I'd hate to end up rewriting somebody else's wheel.

Update Oct 24, 2008: Thanks, everybody! Consensus is, don't roll your own; spending the time figuring out one of the fully-featured template engines will be worth it in the end. The Big Player in this area is the venerable Template::Toolkit, which Your Mother strongly recommended. HTML::Template was pointed to as a simpler, less complicated system, but Your Mother's point - that having learnt Template::Toolkit, you won't need anything else - is a good one.

Update Oct 27, 2008: I tried out WebMake today, and it was exactly what I wanted! Thanks, TGI! I used it to write a new website for; I've also uploaded the source for the entire website. If you're tackling a similar problem to what I describe above, you'll get an idea of what a WebMake file looks like, and what it can do. Available now at the website mentioned above, or (one version ago) at any reputable CPAN mirror. I'm going to try doing another website with Template::Toolkit, and I'll "report back" once that's done.

Once again, thanks to everyone for their help and advice!

Replies are listed 'Best First'.
Re: Assembling text into websites
by Your Mother (Archbishop) on Oct 23, 2008 at 18:46 UTC

    I have done this many times. Template::Toolkit might feel like overkill but this is an area where it totally shines because it can handle variables and configuration and macros between templates and such so well. IIRC the Badger book itself was typeset/assembled with TT2. There are even command line tools bundled to do just this sort of thing. See Template::Tools::ttree and Template::Tools::tpage and the tutorial docs.

    An aside: never roll your own template engine. We all seem to and it can be fun but unless you are a super smartie, it will ultimately be just a learning exercise because other super smarties have already done it and have lots of devs providing patches, features, docs, samples, etc. The time invested in learning a heavier toolkit will repay you much more handsomely than trying to build one from scratch.

      That's great advice, Your Mother, as well as a ringing endorsement. I'll definitely give it a try.

Re: Assembling text into websites
by ccn (Vicar) on Oct 23, 2008 at 18:16 UTC

      It is! TMPL_INCLUDE in particular seems like exactly what I was looking for. Thanks for recommending it!

Re: Assembling text into websites
by oko1 (Deacon) on Oct 24, 2008 at 00:04 UTC

    To follow up to what scorpio17 said, I use HTML::Template to generate several of the pages at that way, for various reasons. The generator script can be run as CGI or to create output to a static file. Quoting the relevant section:

    # Output everything including the header if invoked as a CGI script if ( $0 =~ /\.cgi$/ ){ print header; $fh = *STDOUT; } else { # If invoked via symlink (not ending in ".cgi"), write " +ml" open Mirr, ">mirrors.html" or die "Can't open mirrors.html: $!\n"; $fh = *Mirr; } # print $fh start_html( "Linux Gazette - Mirrors and translations page +" ), $t -> output, end_html; print $fh $t -> output, end_html;

    It works well (the result is at, and has for the last five years. A fair track record. :)

    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: Assembling text into websites
by scorpio17 (Canon) on Oct 23, 2008 at 20:37 UTC

    Note that while many dynamic websites do use modules like HTML::Template, you can also use them "offline" to generate pages for static sites. What you do is write your templates, then run your script to generate the pages, then upload the pages to the server. If you ever need to change something, you just repeat the process. The only time you need to have a script running on the server is if the pages change very frequently (stock quotes, news headlines, etc.)

Re: Assembling text into websites
by TGI (Parson) on Oct 23, 2008 at 18:48 UTC

    Years ago I needed to do something similar, and WebMake proved a nice solution.

    TGI says moo

Re: Assembling text into websites
by Bloodnok (Vicar) on Oct 23, 2008 at 18:09 UTC
    Hi ,

    Why not use CSS for the bulk of the replication?

    You only need to define the styles etc. once and the refer to the central definition in each file.

    ... or possibly the template toolkit - whose CPAN name, for the minute, escapes me.

    A user level that continues to overstate my experience :-))


      I'm going to use CSS for consistant style, but I'm not sure you can replicate the navigation bar/common website elements across different pages using CSS alone - unless you use a lot of before-after definitions, which seems cumbersome. Or is there a neat way of doing this I don't know about?

        .oO(Surely not another goons fan:-)

        Off hand, I couldn't say, I'd have to read it up...AFAIR, I'm sure I've done something extremely similar in a past job.

        A user level that continues to overstate my experience :-))
Re: Assembling text into websites
by moritz (Cardinal) on Oct 24, 2008 at 07:19 UTC
    What follows is shameless advertisement for my own offline-CMS (written in Perl, of course).

    Mowyw expects your project to be organized in three directories:

    source/ # here are all pages that should appear in the output includes/ # here are include files and menus online/ # mowyw will put the result of its processing here

    In the simplest case, mowyw will copy a file source/foo.html to online/foo.html, prepend includes/header (you can configure that name, of course) and append includes/footer to that page. Non-HTML-pages are copied verbatim.

    Inside the file simple directives are processed, here are some examples:

    [% include bar %] # includes file includes/bar [% option no-header %] # don't include the header file [% setvar title Page Title %] # Set variable `title' to `Page Title' [% readvar title %] # is substituted by the value of varaible `titl +e'
Re: Assembling text into websites
by LesleyB (Friar) on Oct 24, 2008 at 09:56 UTC

    For simple websites TT2 is imo the way to go so you made a good choice there.

    I use it to construct both 'dynamic' pages e.g. contact forms, as well as static pages for one site I'm responsible for. If/when you get on to CGI and CGI::Formbuilder the latter provides a TT2 engine so you don't lose out on the work.

    I believe both HTML::Template and TT2 transfer well to mod_perl when you get there too

    Good luck!

      I've worked on a similar project... first time over I just opened the files and printed them off. I should warn you though, it's a very 'not-so-elegant' way of doing it.
      I later found CGI::SSI. Give it a spin : CGI::SSI
Re: Assembling text into websites
by arbingersys (Pilgrim) on Oct 25, 2008 at 21:09 UTC
    I'm a little late to the game, I see, and the die already looks cast, but here's a shameless plug for my own template system anyway. If you're looking for simple, mine's as simple as it gets. And, it likes creating online or offline templates about equally.

    It's called Template::Recall. There's an intro on

    (Curse you PerlMonks for distracting me from getting real work done!)

    A blog among millions.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://719103]
Approved by marto
Front-paged by ccn
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-05-28 23:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found