Click here to Skip to main content
Licence CPOL
First Posted 10 Feb 2006
Views 19,293
Downloads 645
Bookmarked 34 times

Web Site Generator

By | 10 Feb 2006 | Article
A program that uses templates and an XML file to generate a web site.

Introduction

In a moment of weakness, I promised a couple of friends to take care of their web site. At first, it was a very small site but it kept on growing, and the time it took me to keep it up to date grew with it. The first thing that took time was to get all the internal links right when pages were moved around within the site. The second thing was updating the meta-data in an ever growing number of pages.

I needed a tool. There are, of course, a number of tools to be found but they where either too limited or too complex. So I decided to write my own. I didn't mind because writing a small piece of software like this is an excellent opportunity to learn new techniques. The first version was written in Java using Tomcat, Struts, and the Freemarker template library. The second version, this one, is written in C#, using the Ader Template Engine and an XML-file with data about the site.

Description

  1. The program first copies all relevant files from the data directory to the web directory.
  2. Secondly, it loads the main.template into the Ader TemplateManager.
  3. It then iterates through the <page>-tags in the XML-file, loading code-snippets into the main.template.
  4. The expanded template is copied to the Web directory as an HTML-file.

The XML-file

  • ExcludeExtensions

    The file extensions are not copied from the data directory to the web directory.

  • <page path="/demo/pages">

    All pages in the path="/demo/pages" get a menu with the links within the <page>-tag.

<?xml version="1.0" encoding="iso-8859-1"?>

<fsm>
   <init item="DataDir" path="C:/My Documents/mysite"/>
   <init item="WebDir" path="C:/Inetpub/wwwroot"/>
   <init item="ExcludeExtensions" path=".template;.scc"/>

   <pages>
      <!-- The top page -->
      <page path="/demo/pages">
         <link text="About my site" path="/demo/pages" page="intro_main.html"/>
         <link text="Info" path="/demo/pages" page="info_main.html"/>
         <banner text=""/>
         <banner text="Links"/>
         <link text="More" path="/demo/pages/more" page="intro.html"/>
         <link text="Even more" path="/demo/pages/more" page="info.html"/>
      </page>

      <!-- More pages -->
      <page path="/demo/pages/more">
         <link text="About my site" path="/demo/pages" page="intro_main.html"/>
         <link text="Info" path="/demo/pages" page="info_main.html"/>
         <banner text=""/>
         <banner text="More Links"/>
         <link text="More" path="/demo/pages/more" page="intro.html"/>
         <link text="Even more" path="/demo/pages/more" page="info.html"/>
      </page>
   </pages>
</fsm>

Using the Ader TemplateManager

  1. Create a reference to the class Constants. This class has only static methods.
  2. Create a reference to the present node.
  3. Iterate through the links and banners.

See the Ader TemplateEngine for an explanation of the syntax.

<ad:set name="Constants" value="#createtypereference("Constants")#" />
<ad:set name="node" value="#xml#" />

<ad:if test="#Constants.GetData("Path") is 
              node.Attributes.GetNamedItem("path").Value #">
   <ad:foreach collection="#node.ChildNodes#" var="link">
      <ad:set name="attrs" value="#link.Attributes#" />
      <ad:if test="#link.Name is "link" #">
         <a href="#attrs.GetNamedItem("path").Value#/
                  #attrs.GetNamedItem("page").Value#" 
            class="menu">#attrs.GetNamedItem("text").Value#</a><br>
      <ad:elseif test="#link.Name is "banner" #">
         <b> #attrs.GetNamedItem("text").Value#</b><br>
      </ad:if>
   </ad:foreach>
</ad:if>

Links of interest

History

  • 2006-02-02 - Version 1.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sten Hjelmqvist

Web Developer

Sweden Sweden

Member

I'm a computer consultant at Affecto AB in Stockholm,Sweden and I have been designing software since the early 80's.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 10 Feb 2006
Article Copyright 2006 by Sten Hjelmqvist
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid