2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: cocanwiki_template.ml,v 1.3 2004/09/08 15:46:52 rich Exp $
6 * This module wraps around the Template library. It provides caching
7 * of templates and fills in standard fields on a host-specific basis.
17 try Sys.getenv "COCANWIKI_TEMPLATES"
18 with Not_found -> "/usr/share/cocanwiki/templates" in
20 try (Unix.stat path).Unix.st_kind = Unix.S_DIR
21 with Unix.Unix_error _ -> false in
22 if not (is_dir base) then
23 failwith ("environment variable $COCANWIKI_TEMPLATES " ^
24 "must be set to point to my 'templates' directory " ^
25 "(see README file for more details)");
28 (* Cache of precompiled templates, arranged by full path. *)
29 let cache = Hashtbl.create 32
31 let _get_template filename =
32 let path = base // filename in
33 let stat = Unix.stat path in
34 let mtime = stat.st_mtime in
37 let template, old_mtime = Hashtbl.find cache path in
38 if old_mtime < mtime then (
39 (* The template has changed on disk since it was compiled. Reload. *)
40 let template = Template.template path in
41 Hashtbl.replace cache path (template, mtime);
47 (* Template not seen before, so load it. *)
48 let template = Template.template path in
49 Hashtbl.replace cache path (template, mtime);
52 let get_template (dbh : Dbi.connection) hostid filename =
53 let template = _get_template filename in
55 (* Get standard fields concerning this host from the database. *)