The expert corner

From GeneWeb
Jump to navigation Jump to search
150px-Geographylogo svg.png Language:   English

Template programming (%if; ... %end;)

The template mechanism provides a very powerful mechanism to construct HTML (or other) pages according to the content of a GeneWeb base. This section is not intending to give an exhaustive description of the corresponding language. Through some examples extracted from gw/etc/perso.txt, it provides a glimpse of the possibility. The last two examples are extracted from a template that generates LaTeX rather than HTML.

For further analysis, explore the various other template files, and on last resort, explore the Ocaml source files!!

Programming constructs:

 %define(p1, p2) ... %end;
 %if;(cond) [%elseif;] [%else;] %end;
 %foreach; ... %end;
 %for;i;0;n;... %end;
 %apply;xxxx(p1, p2) or
 %apply; ... [%with;... [%and;] ...%end;]
 %nl; %nn; %sp; %sq;

Functions callable with %apply:

 nth("aaa"/"bbb"/"ccc"/"ddd", n)
 nth_c("abcdef", n)

Arithmetic and booleans:

 +, -, /, *, %, =, !=, <, <=, >, >=, "and", "or", "not", "in"

WARNING: all functions may not be fully available depending on the execution context!

     %if;has_qualifiers;%public_name; %qualifier;
     %else;%public_name; %surname;%end;
     %if;has_qualifiers;%first_name; %qualifier;
     %else;%first_name; %surname;%end;
%define;married_to(sexx, date_place)
 %if;(sexx = 0 or sexx = 2)
   %if;are_married;[*married%t to:::date_place]0%nn;
   %elseif;are_not_married;[*relationship%t to:::date_place]0%nn;
   %elseif;are_engaged;[*engaged%t to:::date_place]0%nn;
   %elseif;is_no_sexes_check;[*relationship%t to:::date_place]0%nn;
   %elseif;is_no_mention;date_place [*with]%nn;
   %if;are_married;[*married%t to:::date_place]1%nn;
   %elseif;are_not_married;[*relationship%t to:::date_place]1%nn;
   %elseif;are_engaged;[*engaged%t to:::date_place]1%nn;
   %elseif;is_no_sexes_check;[*relationship%t to:::date_place]1%nn;
   %elseif;is_no_mention;date_place [*with]%nn;
%if;(bvar.can_send_image != "no" and image = "" and first_name != "?" and surname != "?")
     <li><a href="%prefix;m=SND_IMAGE;i=%index;" class="add_portrait">
            %if;(auto_image_file_name != "")[*modify picture]%else;[*add picture]%end;
%define;hgindex(alxx, snxx, fnxx, occxx)
  %if;(alxx = "0")
    %if;(occxx = "0")
      \index{snxx, fnxx}
      \index{snxx, fnxx (occxx)}
    %if;(occxx = "0")
      \index{alxx, voir snxx, fnxx}
      \index{alxx, voir snxx, fnxx (occxx)}
    {\it {\bf %alias;}}%apply;hgindex(alias, surname, first_name, occ)\\%nl;

This list does not claim to be exhaustive (but pretty close!!). A good way to understand the semantic of these commands is to explore the template files and observe how they are used (grep is your friend).


Note for has_event (v7)

Note that the has_event function does not consider birth, baptism, death and burial as "events" per se (as they are often already listed in the personnal data of the individual). If you want those events to be taken into account in this function, then you have to add has_events=always in your .gwf parameter file.

Note also that if has_events=never, then has_event always returns False.


JavaScript can be inserted anytime within templates. JavaScript code may contain template commands as in:

<script type="text/javascript">
  function over1(xx)
   document.getElementById(xx).style.%if;(evarso="on")outline="#C0FEFF solid 6px"%else;backgroundColor="#CBFECE"%end;;
   return false;
  function out1(xx)
   return false;

Note that GeneWeb does not perform any language interpretation ([text to be translated]) within HTML comments!

Macros (%x)

See Macros for a description of their use.

Below is the result of calling all 26 lower case possible macros: This table is automatically generated by inserting a bit of HTML/GeneWeb code into the notes of one person in your base. The corresponding code is here

Table of GeneWeb macros.

Commands (m=XXX)

List of the commands appearing in URL with the form:


This list does not claim to be exhaustive, and does not "explain" what each command does!

"DAG" *
"DOC" (* obsolete *)
"IM" *
"IMH" *
"MOD_WDOC"  (* obsolete *)
"MOD_WDOC_OK"  (* obsolete *)
"PS" P
"RLM" *
"SRC" *
"WDOC" (* obsolete *)

Commands marked with a * are documented below:

Gwd controlled variables


Variables (evar and bvar)

GeneWeb proposed a general purpose mechanism allowing the user/programmer to extend at will the notion of variable described here.

If a new variable is defined in the .gwf configuration file, then it will be possible to test for the value of this variable in the context of a template file. For instance, if we have added


in .gwf, then the following code in the template will be able to perform some specific action:

%if; (bvar.new_var = "sometext" ) 
    ... perform some specific action ...

Similarly, if we have added a new variable in the URL of the query to GeneWeb (localhost:2317/base?p=first-name;n=last-name;lang=fr;tree=3, then it is possible to test for this variable with code such as:

%if; (evar.tree = 3 ) 
    ... display 3 generation tree ...

The value itself of such variables can be inserted in the body of a template by typing %evar.tree;, for instance:

<title>Tree with %evar.tree; generations</title>

The special variable var is treated in the following fashion within notes, sources and comments: If you define in the .gwf file a variable var_text=some_text, then invoquing the macro %vtext; in a note or a template will produce the text some_text as in the following example:

var_who=Bond, James Bond

"My name is %vwho;." in a note will be transformed into "My name is Bond, James Bond.".

This feature is used by some to facilitate signature of sources. The .gwf file contains a list of var_nickname definitions whose value is a full name and possibly a pointer to a page containing more detailed information:

var_henri=<a href="http://site/base?p=Firstname;n=lastname">First_name Lastname</a>

(no %x or %macro; in the value of the parameter!!).

Building trees through direct url construction

See trees or trees/fr

GeneWeb Manual

Rembrandt Old Man Reading a Book.jpg

Use and manage genealogical databases

Technical annex