1 <?php rcs_id('$Id: Template.php,v 1.32 2002-01-28 01:01:26 dairiki Exp $');
3 require_once("lib/ErrorManager.php");
4 require_once("lib/WikiPlugin.php");
14 function Template ($name, &$request, $args = false) {
17 $this->_request = &$request;
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) {
60 $loader = new WikiPluginLoader;
62 $this->_print($loader->expandPI($pi, $this->_request));
65 function _print ($val) {
66 if (isa($val, 'Template'))
67 $this->_expandSubtemplate($val);
72 function _expandSubtemplate (&$template) {
74 if (!$template->_request)
75 $template->_request = &$this->_request;
77 echo "<!-- Begin $template->_name -->\n";
78 // Expand sub-template with defaults from this template.
79 $template->printExpansion($this->_vars);
80 echo "<!-- End $template->_name -->\n";
84 * Substitute HTML replacement text for tokens in template.
86 * Constructs a new WikiTemplate based upon the named template.
90 * @param $token string Name of token to substitute for.
92 * @param $replacement string Replacement HTML text.
94 function replace($varname, $value) {
95 $this->_locals[$varname] = $value;
99 function printExpansion ($defaults = false) {
100 if (!is_array($defaults))
101 $defaults = array('CONTENT' => $defaults);
102 $this->_vars = array_merge($defaults, $this->_locals);
103 extract($this->_vars);
105 $request = &$this->_request;
106 $user = &$request->getUser();
107 $page = &$request->getPage();
109 global $Theme, $RCS_IDS;
111 //$this->_dump_template();
113 global $ErrorManager;
114 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
116 eval('?>' . $this->_munge_input($this->_tmpl));
118 $ErrorManager->popErrorHandler();
121 function getExpansion ($defaults = false) {
123 $this->printExpansion($defaults);
124 $xml = ob_get_contents();
129 function printXML () {
130 $this->printExpansion();
134 return $this->getExpansion();
139 function _dump_template () {
140 $lines = explode("\n", $this->_munge_input($this->_tmpl));
143 foreach ($lines as $line)
144 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
148 function _errorHandler($error) {
149 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
153 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
154 $error->errfile = "In template '$this->_name'";
155 // Hack alert: Ignore 'undefined variable' messages for variables
156 // whose names are ALL_CAPS.
157 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
161 $error->errfile .= "(In template '$this->_name'?)";
163 $lines = explode("\n", $this->_tmpl);
165 if (isset($lines[$error->errline - 1]))
166 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
174 * This is a convenience function and is equivalent to:
179 function Template($name, $args = false) {
181 return new Template($name, $request, $args);
185 * Make and expand the top-level template.
188 * @param $content mixed html content to put into the page
189 * @param $title string page title
190 * @param $page_revision object A WikiDB_PageRevision object
191 * @param $args hash Extract args for top-level template
193 * @return string HTML expansion of template.
195 function GeneratePage($content, $title, $page_revision = false, $args = false) {
198 if (!is_array($args))
201 $args['CONTENT'] = $content;
202 $args['TITLE'] = $title;
203 $args['revision'] = $page_revision;
205 if (!isset($args['HEADER']))
206 $args['HEADER'] = $title;
208 printXML(new Template('top', $request, $args));
216 // c-hanging-comment-ender-p: nil
217 // indent-tabs-mode: nil