3.6 Towards an Improved Header

We've just included a site index in every page to help users to jump from page to page. Now, we are going to carry out a small improvement to site's navigability providing a visual indication of the currently selected page.

In practice, the index entry corresponding to current page will be highlighted by enclosing it between '*' characters. Also, the link will be disabled, since it would point to the current page.

Let's divide the work into two steps. In this step, we'll replace current header, which is a plain HTML file, with a script displaying exactly the same HTML code. In the next step, we'll work on the script in order to add the new feature.

3.6.1 Covered Topics

This section will teach you

3.6.2 Writing Sources

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

Remove the old header, lib/header_html, and create a file called lib/header.cws containing the following script:

File lib/header.cws:

<cows> // __NO_OUTPUT__

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 []) {
  print ("| <a href='" + item + ".html'>" + $(item + "_name") + "</a>");
}

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

</cows>

The script starts defining an array called section []; for the moment, you can simply think an array as a list of strings. We'll use these strings as 'labels' for site's sections. Labels correspond to pages' file names.

After that, we define sections' descriptive names using a simple convention: descriptive name is stored in a variable whose name is given by the label followed by _name.

Then we find a foreach loop, whose general form is:

foreach (item in array_name [ ]) {
  statements-block
}

Statements block is executed once for every element in array_name. Inside statements block, current array element is available as variable item.

Inside the loop a very important element of Cows language is used: the $ () function. Its argument is evaluated and reduced to a string. The $ function is then replaced by the value of a variable with such a name. You can use the $ function everywhere a standard variable is allowed.

This feature gives a high level of flexibility since you can build variable names within your scripts.

In practice, this script loops over sections and for each of them creates a link to corresponding page. As an example, during the first iteration of the loop item's value is: index. So, item + "_name" will evaluate to index_name and $(item + "_name") will evaluate to the value of variable index_name: Animal Rights.

So, the print () function inside foreach loop:

print ("| <a href=\"" + item + ".html\">" + $(item + "_name") + "</a>");
will output:
| <a href="animal_rights.html>Animal Rights<"/a>


3.6.3 Creating Resulting Files

Like in the previous step, we just changed site's architecture: header is no longer a plain HTML file but a G-Cows script so we have to modify all files including it by replacing:

verbatim ("lib/header_html");

with:

include ("lib/header.cws");

Now, we have to clean our project, create a new Makefile, and recreate the site:

$ make clean

rm -f entertainment.html\
vivisection.html\
furs.html\
hunting.html\
index.html\
vegetarianism.html\

$ cows-mkgen
$ make

cows   entertainment.cws entertainment.html
cows   vivisection.cws vivisection.html
cows   furs.cws furs.html
cows   hunting.cws hunting.html
cows   index.cws index.html
cows   vegetarianism.cws vegetarianism.html

Again, you had to rebuild the whole site and recreate Makefile because of the decision to replace a verbatim included HTML file with a G-Cows script.

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