GURGLE Tutorial: Multifile Output

Next we decide that we want a page generated for each person in the phone directory database with only their details on it. So the definition file must essentially produce multiple output files, one for each record. The definition file to do this is included below. Copy it into a file in the same directory as the data source file and call it home1.grg or download home1.grg.

%%database "phonedir.txt"
%%define MKDIR
%%define DELIM |
%%define NAMCOL
%%define TEXEXT .html
%%sorton %LASTNAME %FIRSTNAME
%%equate eq_init
  "/dev/null" >> _eq_outfile
%%equate eq_pre_record
  "home1/"+%FIRSTNAME+"_"+%LASTNAME+_eq_extn >> _eq_outfile
%%record
<HTML>
<HEAD>
<TITLE>%FIRSTNAME %LASTNAME</TITLE>
</HEAD>
<BODY>
<H1>%FIRSTNAME %LASTNAME</H1>
<DL>
  <DT><STRONG>JOB</STRONG></DT>
  <DD>%JOB</DD>
  <DT><STRONG>CONTACT</STRONG></DT>
  <DD>%ROOM, x#isprivate(%EXTENSION)</DD>
</DL>
</TABLE>
</BODY>
</HTML>
%%equate isprivate(extn)
  if extn'0 = 52 then outputs("PRIVATE")
  else outputs(extn)
  endif
%%end

In the above we have added a couple of trigger equates. The one for eq_init initialises the output file to /dev/null (nowhere). The second, eq_pre_record, changes the output file at the start of each record. The record then contains the whole HTML for a single page. The HEADER and FOOTER directives are not used as they apply at the start and at the end of all the records, not for every record.

The output file now includes a constructed name based on the values of the LASTNAME and FIRSTNAME fields (specific to each record). It also contains a directory path, and MKDIR is declared so that the path will be built automatically if the directory doesn't exist. The output file also includes the value of an internal system variable which is the default file extension (defined by TEXEXT).

Now we need to gurgle it to produce all the web pages.

gurgle home1.grg

If everything worked this will produce a home1 directory and lots of separate HTML files within it. This should look like the web pages at home1/.

Exercises

Spruce up the web pages. Try without the eq_init to see why its needed. Update the file in the previous tutorial which generates the whole phone directory as a single page so that the peoples names become links to their specific files in the home1/ directory. Then change the output file so it is written to home1/index.html instead of webphone.html.

NEXT