3.7 Improved Header

3.7.1 Covered Topics

3.7.2 Writing Sources

The sources described in this section can be found inside directory tutorial-5 in the examples archive downloadable from http://www.g-cows.org/.

Actually, every page displays the same header. The next improvement consists in having the link corresponding to the including page disabled and highlighted. So, we are going to modify header.cws in order to introduce a different behavior depending on the including page.

Of course we also need to tell header.cws where it's included from. This is very easy to do, since Cows allows to pass an arbitrary number of parameters to included scripts. As an example, vivisection.cws will include lib/header.cws as follows:

include ("lib/header.cws", "vivisection");

In other words, the header knows where it's included from. Parameters are stored in an array called _argv [ ], whose first element is the name of the script itself. Variable _argc represents the length of this array so if you pass a single parameter _argc will evaluate to 2 and parameter will be stored in _argv [1] (_argv [0] is always the name of the script).

Now, it's very easy to modify the script in order to reach our goal:

File lib/header.cws:

<cows> // __NO_OUTPUT__

if (_argc != 2)
  raiseerror ("script needs 1 parameter");

sec = _argv [1];

sections [] = { "index",   "vegetarianism", "vivisection",
                "hunting", "furs",          "entertainment" };

index_name         = "Animal Rights";
vegetarianism_name = "Vegetarianism";
vivisection_name   = "Vivisection";
hunting_name       = "Hunting";
furs_name          = "Furs & Leather";
entertainment_name = "Animals in Entertainment";

print ("<center>");

foreach (item in sections []) {
   if (item == sec) {
    print ("| *" + $(item+"_name") + "*");
  } else {
    print ("| <a href='" + item + ".html'>" + $(item + "_name") + "</a>");
  }
}

print ("|\n</center>\n<hr>");

</cows>

The if-else statement executes a block or another whenever a given condition is true or false. While looping on sections, if the currently processed one equals to sec, output has the form:

*Furs & Leather*

otherwise, the standard link is produced:

<a href="furs.html">Furs & Leather</a>

Every page must be modified in order to provide a value for sec. Let's only see it for furs.cws.

File furs.cws:

<html>
<head><title>Furs & Leather</title></head>

<body>

<cows include ("lib/header.cws", "furs"); />

<h1>Furs & Leather</h1>

<p align=right>
I haven't bought any leather articles for a<br>
very long time. My ideal is to be able to avoid<br>
all animal products, in food as well as clothing.<br>
<em>-- Martina Navratilova</em>
</p>

<cows include ("lib/footer.cws"); />

</body>
</html>

3.7.3 Creating Resulting Files

This time, we just changed some .cws file, without affecting site's architecture, so we don't need to update the makefile. This can seem very complicated at first, so you'd better see Section 6.3 for details.

For the moment, simply remember that makefiles stores dependencies among files so, whenever a change results in different updating rules, the makefile must be regenerated. Of course, even when you add or remove a page makefile must be updated since a new rule must be added or removed from makefile.

$ make
cows   vegetarianism.cws vegetarianism.html
cows   vivisection.cws vivisection.html
cows   hunting.cws hunting.html
cows   furs.cws furs.html
cows   index.cws index.html
cows   animal_rights.cws animal_rights.html
cows   entertainment.cws entertainment.html

Correctly, all pages have been updated. Let's see one of them, both the source and Lynx's output:

File furs.html:

<html>
<head><title>Furs & Leather</title></head>

<body>

<center>
| <a href="index.html">Animal Rights</a>
| <a href="vegetarianism.html">Vegetarianism</a>
| <a href="vivisection.html">Vivisection</a>
| <a href="hunting.html">Hunting</a>
| *Furs & Leather*
| <a href="entertainment.html">Animals in Entertainment</a>
|
</center>
<hr>


<h1>Furs & Leather</h1>

<p align=right>
I haven't bought any leather articles for a<br>
very long time. My ideal is to be able to avoid<br>
all animal products, in food as well as clothing.<br>
<em>-- Martina Navratilova</em>
</p>

<hr>
Updated: 5 July 2004
 [ <a href="mailto:webmaster@your-domain">webmaster@your-domain</a> ]


</body>
</html>
    | Animal Rights | Vegetarianism | Vivisection | Hunting |
          *Furs & Leather* | Animals in Entertainment |
     _______________________________________________________

                           Furs & Leather

                    I haven't bought any leather articles for a
                very long time. My ideal is to be able to avoid
              all animal products, in food as well as clothing.
                                         -- Martina Navratilova
     _______________________________________________________

   Updated: 5 July 2004 [ webmaster@your-domain ]

3.7.4 Conclusions

We've just reached a good result: we have a site, with an header which can easily be turned into a fancy navigation bar (add some graphics and use your creativity...). Every page also shows the date of last modify.

Since now, we can change site's contents and simply run make in order to have the site updated. Moreover, resulting site is completely made of static HTML files, so it can be hosted on every server and quickly delivered to users.

The same result can be achieved with a server side scripting language, but this would be useless: why generating navigation bars again and again, as users request the pages when they can be created once? Of course, if some elements need to be created in real time you must switch to a server side language. Even better, you can mix Cows and server side languages in the same pages, and use the former for contents that can be created once, the latter for dynamic elements.

This manual can be downloaded from http://www.g-cows.org/.