1 (** Wrapper around Perl [Template] class (Template Toolkit).
3 * Author: Dave Benjamin <dave@3dex.com>.
8 let _ = eval "use Template"
10 exception Not_implemented
11 exception Error of string
24 | Hash of (string * t) list;;
28 let rec sv_of_variant = function
29 | Variant.Null -> sv_undef ()
30 | Variant.String s -> sv_of_string s
31 | Variant.Int i -> sv_of_int i
32 | Variant.Float f -> sv_of_float f
33 | Variant.Bool b -> sv_of_bool b
34 | Variant.Array xs -> arrayref (av_of_sv_list (List.map sv_of_variant xs))
35 | Variant.Hash xs -> hashref
36 (let hv = hv_empty () in
37 List.iter (fun (k, v) -> hv_set hv k (sv_of_variant v)) xs;
41 let hv_of_string_pair_list pairs =
42 let hv = hv_empty () in
43 List.iter (fun (k, v) -> hv_set hv k (sv_of_string v)) pairs;
50 method process file vars =
51 let output = sv_of_string "" in
52 let args = [sv_of_string file; sv_of_variant vars; scalarref output] in
53 let result = call_method sv "process" args in
54 if not (sv_is_true result) then
55 raise (Error self#error)
60 string_of_sv (call_method sv "error" [])
64 let may f = function None -> () | Some v -> f v
66 let new_ ?start_tag ?end_tag ?tag_style ?pre_chomp ?post_chomp ?trim
67 ?interpolate ?anycase ?include_path ?delimiter ?absolute ?relative
68 ?default ?blocks ?auto_reset ?recursion ?variables ?constants
69 ?constant_namespace ?namespace ?pre_process ?post_process ?process
70 ?wrapper ?error ?errors ?eval_perl ?output ?output_path ?debug ?debug_format
71 ?cache_size ?compile_ext ?compile_dir ?plugins ?plugin_base ?load_perl
72 ?filters ?v1dollar ?load_templates ?load_plugins ?load_filters
73 ?tolerant ?service ?context ?stash ?parser ?grammar () =
77 args := sv_of_string "START_TAG" :: sv_of_string v :: !args) start_tag;
79 args := sv_of_string "END_TAG" :: sv_of_string v :: !args) end_tag;
81 args := sv_of_string "TAG_STYLE" :: sv_of_string v :: !args) tag_style;
83 args := sv_of_string "PRE_CHOMP" :: sv_of_bool v :: !args) pre_chomp;
85 args := sv_of_string "POST_CHOMP" :: sv_of_bool v :: !args) post_chomp;
87 args := sv_of_string "TRIM" :: sv_of_bool v :: !args) trim;
89 args := sv_of_string "INTERPOLATE" :: sv_of_bool v :: !args) interpolate;
91 args := sv_of_string "ANYCASE" :: sv_of_bool v :: !args) anycase;
93 args := sv_of_string "INCLUDE_PATH" :: arrayref (av_of_string_list v) :: !args) include_path;
95 args := sv_of_string "DELIMITER" :: sv_of_string v :: !args) delimiter;
97 args := sv_of_string "ABSOLUTE" :: sv_of_bool v :: !args) absolute;
99 args := sv_of_string "RELATIVE" :: sv_of_bool v :: !args) relative;
101 args := sv_of_string "DEFAULT" :: sv_of_string v :: !args) default;
103 args := sv_of_string "BLOCKS" :: hashref (hv_of_string_pair_list v) :: !args) blocks;
105 args := sv_of_string "AUTO_RESET" :: sv_of_bool v :: !args) auto_reset;
107 args := sv_of_string "RECURSION" :: sv_of_bool v :: !args) recursion;
109 args := sv_of_string "VARIABLES" :: sv_of_variant v :: !args) variables;
111 args := sv_of_string "CONSTANTS" :: sv_of_variant v :: !args) constants;
113 args := sv_of_string "CONSTANT_NAMESPACE" :: sv_of_string v :: !args) constant_namespace;
115 args := sv_of_string "NAMESPACE" :: sv_of_variant v :: !args) namespace;
117 args := sv_of_string "PRE_PROCESS" :: arrayref (av_of_string_list v) :: !args) pre_process;
119 args := sv_of_string "POST_PROCESS" :: arrayref (av_of_string_list v) :: !args) post_process;
121 args := sv_of_string "PROCESS" :: arrayref (av_of_string_list v) :: !args) process;
123 args := sv_of_string "WRAPPER" :: arrayref (av_of_string_list v) :: !args) wrapper;
125 args := sv_of_string "ERROR" :: sv_of_string v :: !args) error;
127 args := sv_of_string "ERRORS" :: hashref (hv_of_string_pair_list v) :: !args) errors;
129 args := sv_of_string "EVAL_PERL" :: sv_of_bool v :: !args) eval_perl;
131 raise Not_implemented) output;
133 raise Not_implemented) output_path;
135 args := sv_of_string "DEBUG" :: sv_of_string v :: !args) debug;
137 args := sv_of_string "DEBUG_FORMAT" :: sv_of_string v :: !args) debug_format;
139 args := sv_of_string "CACHE_SIZE" :: sv_of_int v :: !args) cache_size;
141 args := sv_of_string "COMPILE_EXT" :: sv_of_string v :: !args) compile_ext;
143 args := sv_of_string "COMPILE_DIR" :: sv_of_string v :: !args) compile_dir;
145 args := sv_of_string "PLUGINS" :: hashref (hv_of_string_pair_list v) :: !args) plugins;
147 args := sv_of_string "PLUGIN_BASE" :: arrayref (av_of_string_list v) :: !args) plugin_base;
149 args := sv_of_string "LOAD_PERL" :: sv_of_bool v :: !args) load_perl;
151 raise Not_implemented) filters;
153 args := sv_of_string "V1DOLLAR" :: sv_of_bool v :: !args) v1dollar;
155 raise Not_implemented) load_templates;
157 raise Not_implemented) load_plugins;
159 raise Not_implemented) load_filters;
161 args := sv_of_string "TOLERANT" :: sv_of_bool v :: !args) tolerant;
163 raise Not_implemented) service;
165 raise Not_implemented) context;
167 raise Not_implemented) stash;
169 raise Not_implemented) parser;
171 raise Not_implemented) grammar;
172 let sv = call_class_method "Template" "new" !args in