1 <?php rcs_id('$Id: Template.php,v 1.51 2003-12-20 23:54:15 carstenklapp Exp $');
3 require_once("lib/ErrorManager.php");
13 function Template ($name, &$request, $args = false) {
16 $this->_request = &$request;
18 $this->_basepage = $request->getArg('pagename');
20 $file = $Theme->findTemplate($name);
21 $fp = fopen($file, "rb");
22 $this->_tmpl = fread($fp, filesize($file));
26 $this->_locals = $args;
28 $this->_locals = array('CONTENT' => $args);
30 $this->_locals = array();
33 function _munge_input($template) {
35 // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
36 $orig[] = '/<\?plugin.*?\?>/se';
37 $repl[] = "\$this->_mungePlugin('\\0')";
39 // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
40 $orig[] = '/<\?=(.*?)\?>/s';
41 $repl[] = '<?php $this->_print(\1);?>';
43 return preg_replace($orig, $repl, $template);
46 function _mungePlugin($pi) {
47 // HACK ALERT: PHP's preg_replace, with the /e option seems to
48 // escape both single and double quotes with backslashes.
49 // So we need to unescape the double quotes here...
51 $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
52 return sprintf('<?php $this->_printPlugin(%s); ?>',
53 "'" . str_replace("'", "\'", $pi) . "'");
56 function _printPlugin ($pi) {
57 include_once("lib/WikiPlugin.php");
61 $loader = new WikiPluginLoader;
63 $this->_print($loader->expandPI($pi, $this->_request, $this->_basepage));
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;
77 if (defined('DEBUG') and DEBUG) {
78 echo "<!-- Begin $template->_name -->\n";
80 // Expand sub-template with defaults from this template.
81 $template->printExpansion($this->_vars);
82 if (defined('DEBUG') and DEBUG) {
83 echo "<!-- End $template->_name -->\n";
88 * Substitute HTML replacement text for tokens in template.
90 * Constructs a new WikiTemplate based upon the named template.
94 * @param $token string Name of token to substitute for.
96 * @param $replacement string Replacement HTML text.
98 function replace($varname, $value) {
99 $this->_locals[$varname] = $value;
103 function printExpansion ($defaults = false) {
104 if (!is_array($defaults))
105 $defaults = array('CONTENT' => $defaults);
106 $this->_vars = array_merge($defaults, $this->_locals);
107 extract($this->_vars);
109 $request = &$this->_request;
111 $user = &$request->getUser();
113 $page = &$request->getPage();
115 global $Theme, $RCS_IDS;
119 //$this->_dump_template();
121 global $ErrorManager;
122 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
124 eval('?>' . $this->_munge_input($this->_tmpl));
126 $ErrorManager->popErrorHandler();
129 function getExpansion ($defaults = false) {
131 $this->printExpansion($defaults);
132 $xml = ob_get_contents();
137 function printXML () {
138 $this->printExpansion();
142 return $this->getExpansion();
147 function _dump_template () {
148 $lines = explode("\n", $this->_munge_input($this->_tmpl));
151 foreach ($lines as $line)
152 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
156 function _errorHandler($error) {
157 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
160 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
161 $error->errfile = "In template '$this->_name'";
162 // Hack alert: Ignore 'undefined variable' messages for variables
163 // whose names are ALL_CAPS.
164 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
168 $error->errfile .= " (In template '$this->_name')";
170 $lines = explode("\n", $this->_tmpl);
171 if (isset($lines[$error->errline - 1]))
172 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
180 * This is a convenience function and is equivalent to:
185 function Template($name, $args = false) {
187 return new Template($name, $request, $args);
191 * Make and expand the top-level template.
194 * @param $content mixed html content to put into the page
195 * @param $title string page title
196 * @param $page_revision object A WikiDB_PageRevision object
197 * @param $args hash Extract args for top-level template
199 * @return string HTML expansion of template.
201 function GeneratePage($content, $title, $page_revision = false, $args = false) {
204 if (!is_array($args))
207 $args['CONTENT'] = $content;
208 $args['TITLE'] = $title;
209 $args['revision'] = $page_revision;
211 if (!isset($args['HEADER']))
212 $args['HEADER'] = $title;
214 printXML(new Template('html', $request, $args));
219 * For dumping pages as html to a file.
221 function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
224 if (!is_array($args))
227 $args['CONTENT'] = $content;
228 $args['TITLE'] = split_pagename($title);
229 $args['revision'] = $page_revision;
231 if (!isset($args['HEADER']))
232 $args['HEADER'] = split_pagename($title);
234 global $HIDE_TOOLBARS, $NO_BASEHREF, $HTML_DUMP;
235 $HIDE_TOOLBARS = true;
238 $html = asXML(new Template('htmldump', $request, $args));
240 $HIDE_TOOLBARS = false;
250 // c-hanging-comment-ender-p: nil
251 // indent-tabs-mode: nil