APGen Documentation Previous Topic: Part 4: Creating New Pages Next Topic: Glossary Parent Topic: Web Authoring Examples    Web Authoring Examples
Part 5: Multiple Templates and Multiple Content Types
See Also:

This example adds multiple templates and the capability to merge with page content stored in text files, HTML files, HTML fragments, and APG scripts.  This results in flexibility of content format and page appearance, improved maintenance, and great performance.

Example files for this topic are found in

Program Files\APGen\Examples\Aut_Auth\Streams

The example files were copied from the Aut_Auth\NewPages directory.  Then the changes described below were applied.

Motivation

The web authoring system set up in the previous 4 topics is a bit simple for real world web sites.  Real world web sites will have different sections within the site, a number of different looks and feels, plus dynamic pages.  (Actually, the previous system supports ASP pages).  In addition, it is desirable to be able to handle content pages in different formats - the previous system requires all content pages to be APG scripts.

This new system addresses these issues.

Implementation

This example revises the web site and directory layout to accomodate the new functionality.  The new directory structure is:

\Content\ Holds the content files.  These can be text files, HTML files, HTML fragments, and APG scripts.  This example can be extended to handle other content file formats.
\Log\ Holds the log files.  If errors occur, log files are created here.
\Output\ The generated web site is written here.
\Resources\ Holds resource files, like CSS files and images.  When the site is built, these resource files are copied to the \Output\ dir.
\Templates\ Contains the templates that are applied to each content file.  Each template handles a single content file format, and can generate its own look and feel around the page content.
\Templates\Include\ Include files used by template files.  These APG scripts contain routines that may be shared between multiple template files.

In this example, all four templates generate pages that are similar in appearance, but they each process different content formats.  For real web sites, you will want to add more templates to handle additional content file formats, and to add new "looks".

Site.xml has been modified so that each webpage element specifies its own template and content file.  For example:

<webpage title="The Log Object" fname="objlog.htm" content="objlog.htm" template="htmlfragment.apg" navbartitle="Log Object">

Build.apg has been modified to run the template APG scripts specified by each <webpage>.  The <webpage> attributes, including the content filename, are passed to the template.  Each template is responsible for generating the resulting web page, including extracting the content from the content file, and inserting the content in the output file.

To generate the web site, run build.apg.

New and Changed Files
site.xml Changed so that each <webpage> tag has content and template attributes.  These are used to specify the content file and the template file for each web page.
build.apg Changed so that each page is generated by executing the specified template APG script.  The <webpage> attributes are passed to the template APG script.
content/hello.txt A text file containing the content for hello.htm.
content/objlog.htm An HTML page fragment containing the content for objlog.htm.  This is not a complete HTML file - it is a page fragment, which is inserted as-is into the template content.
content/logfile.htm A Microsoft Word-generated HTML page containing the content for logfile.htm.  This is a complete HTML file - the template for this page parses out the body and CSS styles and merges them with the template content.
content/logwrite.apg An APG script containing the content for generating logwrite.htm.  This content file contains APG script that is executed at build time.  The output of this content script is merged with output from the template script.
templates/textfragment.apg A template script that handles text content files.  The text file is HTMLEncoded so spacing and special characters are preserved.  The content is inserted in the standard HTML framing.
templates/htmlfragment.apg A template script that handles HTML fragment content files. There is no special parsing or manipulation of the HTML fragment, so HTML tags are formatted according to the styles of the template.  The content is inserted in the standard HTML framing.
templates/htmlparser.apg A template script that handles HTML content files, including those generated by Microsoft Word.  The HTML is parsed and merged with the template content.  CSS styles in the content file are scoped using a #idPageContent prefix so they don't conflict with styles in the template.  These scoped CSS styles from the content file are inserted in the <HEAD> next to the CSS styles from the template.  The <BODY> of the content file is inserted in a <DIV id=#idPageContent> tag in the output file.  This way, the CSS styles from the template are applied to the template HTML, and the CSS styles from the content file are applied to the content HTML.

This HTML parser can handle HTML content documents generated by Microsoft Word, Microsoft Excel, FrontPage, or many other web page authoring tools.
templates/apgenstream.apg A template script that handles APG script content files.  The HTML generated by the content file APG script is inserted in the standard HTML framing.  The content APG script can contain any amount of APG script, including database access.  The output from the content script is directed to the output file created by the template script using APGen's pluggable streams feature.
templates/include/htmlparser.apg An include file containing Sub ParseHTMLFile, which uses regular expressions to extract the styles and body of an HTML content file and merge it with a template.  This routine is used by the htmlparser.apg template, and can be used by your own templates to merge with HTML and Microsoft Office documents.
templates/include/navbar.apg This include file contains code to render the navigation bar using data from site.xml.  All of the templates use the navbar.apg include file to render the navbar section of the page.

Taking it Further

This example provides much of the power and flexibility needed by real-world web sites.  At the same time, maintenance is easy (thus inexpensive), and the performance of the resulting site is phenomenal.  The biggest shortcoming of this system is that adding and updating pages requires some technical knowledge about how this system works.  Further improvements should allow less sophisticated users to add and edit web site content using a web-based administrator interface (content management).  Improvements could include: