1 <?php rcs_id('$Id: Template.php,v 1.31 2002-01-25 00:32:39 dairiki Exp $');
3 require_once("lib/ErrorManager.php");
4 require_once("lib/WikiPlugin.php");
5 require_once("lib/ButtonFactory.php");
15 function Template ($name, &$request, $args = false) {
18 $this->_request = &$request;
21 $file = $Theme->findTemplate($name);
22 $fp = fopen($file, "rb");
23 $this->_tmpl = fread($fp, filesize($file));
27 $this->_locals = $args;
29 $this->_locals = array('CONTENT' => $args);
31 $this->_locals = array();
34 function _munge_input($template) {
36 // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
37 $orig[] = '/<\?plugin.*?\?>/se';
38 $repl[] = "\$this->_mungePlugin('\\0')";
40 // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
41 $orig[] = '/<\?=(.*?)\?>/s';
42 $repl[] = '<?php $this->_print(\1);?>';
44 return preg_replace($orig, $repl, $template);
47 function _mungePlugin($pi) {
48 // HACK ALERT: PHP's preg_replace, with the /e option seems to
49 // escape both single and double quotes with backslashes.
50 // So we need to unescape the double quotes here...
52 $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
53 return sprintf('<?php $this->_printPlugin(%s); ?>',
54 "'" . str_replace("'", "\'", $pi) . "'");
57 function _printPlugin ($pi) {
61 $loader = new WikiPluginLoader;
63 $this->_print($loader->expandPI($pi, $this->_request));
66 function _print ($val) {
67 if (isa($val, 'Template'))
68 $this->_expandSubtemplate($val);
73 function _expandSubtemplate (&$template) {
75 if (!$template->_request)
76 $template->_request = &$this->_request;
78 echo "<!-- Begin $template->_name -->\n";
79 // Expand sub-template with defaults from this template.
80 $template->printExpansion($this->_vars);
81 echo "<!-- End $template->_name -->\n";
85 * Substitute HTML replacement text for tokens in template.
87 * Constructs a new WikiTemplate based upon the named template.
91 * @param $token string Name of token to substitute for.
93 * @param $replacement string Replacement HTML text.
95 function replace($varname, $value) {
96 $this->_locals[$varname] = $value;
100 function printExpansion ($defaults = false) {
101 if (!is_array($defaults))
102 $defaults = array('CONTENT' => $defaults);
103 $this->_vars = array_merge($defaults, $this->_locals);
104 extract($this->_vars);
106 $request = &$this->_request;
107 $user = &$request->getUser();
108 $page = &$request->getPage();
110 global $Theme, $RCS_IDS;
111 $ButtonFactory = new ButtonFactory($request);
113 //$this->_dump_template();
115 global $ErrorManager;
116 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
118 eval('?>' . $this->_munge_input($this->_tmpl));
120 $ErrorManager->popErrorHandler();
123 function getExpansion ($defaults = false) {
125 $this->printExpansion($defaults);
126 $xml = ob_get_contents();
131 function printXML () {
132 $this->printExpansion();
136 return $this->getExpansion();
141 function _dump_template () {
142 $lines = explode("\n", $this->_munge_input($this->_tmpl));
145 foreach ($lines as $line)
146 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
150 function _errorHandler($error) {
151 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
155 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
156 $error->errfile = "In template '$this->_name'";
157 // Hack alert: Ignore 'undefined variable' messages for variables
158 // whose names are ALL_CAPS.
159 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
163 $error->errfile .= "(In template '$this->_name'?)";
165 $lines = explode("\n", $this->_tmpl);
167 if (isset($lines[$error->errline - 1]))
168 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
176 * This is a convenience function and is equivalent to:
181 function Template($name, $args = false) {
183 return new Template($name, $request, $args);
187 * Make and expand the top-level template.
190 * @param $content mixed html content to put into the page
191 * @param $title string page title
192 * @param $page_revision object A WikiDB_PageRevision object
193 * @param $args hash Extract args for top-level template
195 * @return string HTML expansion of template.
197 function GeneratePage($content, $title, $page_revision = false, $args = false) {
200 if (!is_array($args))
203 $args['CONTENT'] = $content;
204 $args['TITLE'] = $title;
205 $args['revision'] = $page_revision;
207 if (!isset($args['HEADER']))
208 $args['HEADER'] = $title;
210 printXML(new Template('top', $request, $args));
218 // c-hanging-comment-ender-p: nil
219 // indent-tabs-mode: nil