|Did you know ...||Search Documentation:|
|pwp.pl -- Prolog Well-formed Pages|
PWP is an approach to server-side scripting using Prolog which is based on a simple key principle:
Especially when generating XML rather than HTML, this is such an obvious thing to do. We have many kinds of XML checking tools.
Having decided that the input should be well formed, that means NO NEW SYNTAX
None of the weird and horrible <% ... %> or whatever not-quite-XML stuff you see in other template systems, making checking so very hard (and therefore, making errors so distressingly common).
That in turns means that PWP "markup" must be based on special elements or special attributes. The fact that an XML parser must allow undeclared attributes on any element even when validating, but must not allow undeclared elements, suggests doing this through attributes. In particular, one should be able to take an existing DTD, such as an XHTML DTD, and just use that without modification. So the design reduces to
This description uses the following name space:
The attributes are
Here's what they mean. Each element is expanded in the context of a set of variable bindings. After expansion, if the tag is not mapped to '-', all attributes in the pwp: namespace are removed and the children elements are recursively expanded.
Term is a Prolog term; variables in Term are bound by the context. An empty Term is regarded as a missing value for this attribute. The Prolog variable CONTEXT refers to the entire context, a list of Name = Value, where Name is a Prolog atom holding the name of the context variable and Value is an arbitrary Prolog term.
The default value for pwp:how is text.
If pwp:tag is missing or the value is empty, the current element appears in the output (after further processing) with its present tag. If pwp:tag is a QName, the current element appears (...) with that as its tag. That option is most useful in DTDs, where an "authoring" DTD may use one tag and have it automatically mapped to another tag in the output, e.g., <item> -> <li>. Finally, if pwp:tag is '-', the children of the current element (either the result of pwp:use or the transformed original children, whichever applies) appear in the output but there is no element around them. A missing or empty pwp:ask is just like pwp:ask = 'true'.
Attributes in the pwp namespace are not affected by this attribute. Such attributes are always stripped out and never substituted into. If pwp:att is missing or empty, attributes of the current element are copied over to the output unchanged. If pwp:att = 'c' for some non-alphanumeric character c, each attribute is examined for occurrences of c(...)c and c[...]c which are as short as possible. There is no one character which could be used every time, so you have to explicitly choose a substitution marker which is safe for the data you do not want to be altered. None of the pwp attributes are inherited, least of all this one. Text outside c(...)c groups is copied unchanged; text inside a c(...)c group is parsed as a Prolog term and treated as if by pwp:how = text. Text inside a c[...]c group is evaluated (in the current context), and if it fails, the entire attribute will be removed from the element.
<html xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl" pwp:ask = "ensure_loaded(msg), once(msg(Greeting))"> <head> <title pwp:use="Greeting"/> </head> <body> <p><span pwp:use="Greeting" pwp:tag='-'/></p> </body> </html>
This example illustrates an important point. Prolog Well-Formed Pages provide NO way to physically incorporate Prolog clauses into a page template. Prolog clauses must be put in separate files which can be checked by a Prolog syntax checker, compiler, cross-referencer, &c WITHOUT the Prolog tool in question needing to know anything whatsoever about PWP. You load the files using pwp:ask on the root element.
<html xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"> <head><title>Example 2</title></head> <body pwp:ask="Hello = 'Hello world', A = 20, B = 22"> <h1 pwp:use="Hello"/> <p>The answer is <span pwp:use="C" pwp:ask="C is A+B"/>.</p> </body> </html>
staff(NickName, FullName, Office, Phone, E_Mail_Address).
status(NickName, full_time | part_time). We want to make a phone list of full time staff.
<html xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl" pwp:ask='ensure_loaded(staff)'> <head> <title>Phone list for Full-Time staff.</title> </head> <body> <h1>Phone list for Full-Time staff.</h1> <table pwp:ask = "setof(FullName-Phone, N^O^E^( status(N, full_time), staff(N, FullName, O, Phone, E) ), Staff_List)"> <tr><th>Name</th><th>Phone</th></tr> <tr pwp:ask="member(FullName-Phone, Staff_List)"> <td pwp:use="FullName"/> <td pwp:use="Phone"/> </tr> </table> </body> </html>
<html xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl" pwp:ask='ensure_loaded(staff)'> <head> <title>Phone list for Full-Time staff.</title> </head> <body> <h1>Phone list for Full-Time staff.</h1> <table pwp:ask = "setof(FullName-E_Mail, N^O^P^staff(N, FullName, O, P, E_Mail), Staff_List)"> <tr><th>Name</th><th>Address</th></tr> <tr pwp:ask="member(FullName-E_Mail, Staff_List)"> <td pwp:use="FullName"/> <td><a pwp:use="E_Mail" pwp:att='$' href="mailto:$(E_Mail)$"/></td> </tr> </table> </body> </html>
<html xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"> <head><title>$SHELL</title></head> <body> <p pwp:ask="getenv('SHELL', Shell)" >The default shell is <span pwp:tag="-" pwp:use="Shell"/>.</p> <p pwp:ask="\+getenv('SHELL',_)">There is no default shell.</p> </body> </html>
There is one other criterion for a good server-side template language:
It should be possible to compile templates so as to eliminate most if not all interpretation overhead.
This particular notation satisfies that criterion with the limitation that the conversion of a term to character data requires run-time traversal of terms (because the terms are not known until run time).