# This is a simple template-handling system. You pass it a big data
# structure with key/value pairs, and a template string to fill out.
#
-# Within a template, it recognizes tags delimited by tildes (~). When
-# the template is filled out, the tags will be replaced with the
-# corresponding data. Tags ending with '?' and '.' mark the start and
-# end of a sub-template (for optional or repeated text), and can be
-# wrapped in HTML comments (which will be removed along with the tags
-# when the template is filled out).
+# Within a template, it recognizes tags of the form ~name [arg...]~,
+# optionally wrapped in HTML comments (which will be removed along with
+# the tag markers when the template is filled out).
+#
+# { and } as the final argument mark those tags as being the start and
+# end of a sub-template (for optional or repeated sections). All other
+# tags represent slots to be directly filled by data values. On a }
+# tag, the name is optional, but must match the corresponding { tag if
+# present.
+#
+# For a value tag, arguments represent encodings to be applied
+# successively. For instance, ~foo html~ will encode it to be safe in
+# HTML ('&' to '&', '<' to '<', and so on).
+#
+# { tags can take one argument, which will call the corresponding
+# tem_auto_* function to munge the data, automating certain common use
+# cases. See the comments on the tem_auto functions for more details.
require_once('code/wfpl/encode.php');
require_once('code/wfpl/file.php');
return parse_template(file_get_contents($filename));
}
-# First we take the template string and break it up into an array
-# of strings and sub-arrays. The first item in a sub-array is the name
-# of the value or sub-template.
+# First we take the template string and convert it into a tree of
+# strings and sub-templates. A template is a hash with a name string,
+# a pieces array, and possibly an args array.
function parse_template($string) {
# Don't mess with the $stack/$tem assignments! Since
array_pop($keychain);
}
array_pop($keychain);
- } else $output .= tem_get_enc($piece, $context);
+ } else { # variable
+ $output .= tem_get_enc($piece, $context);
+ }
}
}
return $output;