2 rcs_id('$Id: Template.php,v 1.56 2004-04-12 13:04:50 rurban Exp $');
4 require_once("lib/ErrorManager.php");
14 function Template ($name, &$request, $args = false) {
17 $this->_request = &$request;
19 $this->_basepage = $request->getArg('pagename');
21 $file = $Theme->findTemplate($name);
23 trigger_error("no template for $name found", E_USER_WARNING);
26 $fp = fopen($file, "rb");
28 trigger_error("$file not found", E_USER_WARNING);
31 $this->_tmpl = fread($fp, filesize($file));
33 //$userid = $request->_user->_userid;
35 $this->_locals = $args;
37 $this->_locals = array('CONTENT' => $args);
39 $this->_locals = array();
42 function _munge_input($template) {
44 // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
45 $orig[] = '/<\?plugin.*?\?>/se';
46 $repl[] = "\$this->_mungePlugin('\\0')";
48 // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
49 $orig[] = '/<\?=(.*?)\?>/s';
50 $repl[] = '<?php $this->_print(\1);?>';
52 return preg_replace($orig, $repl, $template);
55 function _mungePlugin($pi) {
56 // HACK ALERT: PHP's preg_replace, with the /e option seems to
57 // escape both single and double quotes with backslashes.
58 // So we need to unescape the double quotes here...
60 $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
61 return sprintf('<?php $this->_printPlugin(%s); ?>',
62 "'" . str_replace("'", "\'", $pi) . "'");
65 function _printPlugin ($pi) {
66 include_once("lib/WikiPlugin.php");
70 $loader = new WikiPluginLoader;
72 $this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
75 function _print ($val) {
76 if (isa($val, 'Template'))
77 $this->_expandSubtemplate($val);
82 function _expandSubtemplate (&$template) {
84 if (!$template->_request)
85 $template->_request = &$this->_request;
86 if (defined('DEBUG') and DEBUG) {
87 echo "<!-- Begin $template->_name -->\n";
89 // Expand sub-template with defaults from this template.
90 $template->printExpansion($this->_vars);
91 if (defined('DEBUG') and DEBUG) {
92 echo "<!-- End $template->_name -->\n";
97 * Substitute HTML replacement text for tokens in template.
99 * Constructs a new WikiTemplate based upon the named template.
103 * @param $token string Name of token to substitute for.
105 * @param $replacement string Replacement HTML text.
107 function replace($varname, $value) {
108 $this->_locals[$varname] = $value;
112 function printExpansion ($defaults = false) {
113 if (!is_array($defaults))
114 $defaults = array('CONTENT' => $defaults);
115 $this->_vars = array_merge($defaults, $this->_locals);
116 extract($this->_vars);
118 $request = &$this->_request;
120 $user = &$request->getUser();
122 $page = &$request->getPage();
124 global $Theme, $RCS_IDS;
128 //$this->_dump_template();
130 global $ErrorManager;
131 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
133 eval('?>' . $this->_munge_input($this->_tmpl));
135 $ErrorManager->popErrorHandler();
138 function getExpansion ($defaults = false) {
140 $this->printExpansion($defaults);
141 $xml = ob_get_contents();
146 function printXML () {
147 $this->printExpansion();
151 return $this->getExpansion();
156 function _dump_template () {
157 $lines = explode("\n", $this->_munge_input($this->_tmpl));
160 foreach ($lines as $line)
161 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
165 function _errorHandler($error) {
166 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
169 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
170 $error->errfile = "In template '$this->_name'";
171 // Hack alert: Ignore 'undefined variable' messages for variables
172 // whose names are ALL_CAPS.
173 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
177 $error->errfile .= " (In template '$this->_name')";
179 $lines = explode("\n", $this->_tmpl);
180 if (isset($lines[$error->errline - 1]))
181 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
189 * This is a convenience function and is equivalent to:
194 function Template($name, $args = false) {
196 return new Template($name, $request, $args);
200 * Make and expand the top-level template.
203 * @param $content mixed html content to put into the page
204 * @param $title string page title
205 * @param $page_revision object A WikiDB_PageRevision object
206 * @param $args hash Extract args for top-level template
208 * @return string HTML expansion of template.
210 function GeneratePage($content, $title, $page_revision = false, $args = false) {
213 if (!is_array($args))
216 $args['CONTENT'] = $content;
217 $args['TITLE'] = $title;
218 $args['revision'] = $page_revision;
220 if (!isset($args['HEADER']))
221 $args['HEADER'] = $title;
223 printXML(new Template('html', $request, $args));
228 * For dumping pages as html to a file.
230 function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
233 if (!is_array($args))
236 $content->_basepage = $title;
237 $args['CONTENT'] = $content;
238 $args['TITLE'] = split_pagename($title);
239 $args['revision'] = $page_revision;
241 if (!isset($args['HEADER']))
242 $args['HEADER'] = split_pagename($title);
244 global $HIDE_TOOLBARS, $NO_BASEHREF, $HTML_DUMP;
245 $HIDE_TOOLBARS = true;
248 $html = asXML(new Template('htmldump', $request, $args));
250 $HIDE_TOOLBARS = false;
255 // $Log: not supported by cvs2svn $
256 // Revision 1.55 2004/04/02 15:06:55 rurban
257 // fixed a nasty ADODB_mysql session update bug
258 // improved UserPreferences layout (tabled hints)
259 // fixed UserPreferences auth handling
260 // improved auth stability
261 // improved old cookie handling: fixed deletion of old cookies with paths
263 // Revision 1.54 2004/03/02 18:11:39 rurban
264 // CreateToc support: Pass the preparsed markup to each plugin as $dbi->_markup
265 // to be able to know about its context, and even let the plugin change it.
268 // Revision 1.53 2004/02/22 23:20:31 rurban
269 // fixed DumpHtmlToDir,
270 // enhanced sortby handling in PageList
271 // new button_heading th style (enabled),
272 // added sortby and limit support to the db backends and plugins
273 // for paging support (<<prev, next>> links on long lists)
275 // Revision 1.52 2003/12/20 23:59:19 carstenklapp
276 // Internal change: Added rcs Log tag & emacs php mode tag (sorry, forgot
277 // this in the last commit).
284 // c-hanging-comment-ender-p: nil
285 // indent-tabs-mode: nil