Click here to Skip to main content
16,020,990 members
Articles / Programming Languages / XML

Web Site Generator

Rate me:
Please Sign up or sign in to vote.
4.09/5 (2 votes)
10 Feb 2006CPOL2 min read 30.3K   825   35  
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
<?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.

XML
<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)


Written By
Software Developer (Senior) SEB bank
Sweden Sweden
I work as a developer in the 'Risk control' department at SEB bank in Stockholm,Sweden and I have been designing software since the early 80's.

Comments and Discussions

 
-- There are no messages in this forum --