2 rcs_id('$Id: Template.php,v 1.60 2004-06-14 11:31:36 rurban Exp $');
4 require_once("lib/ErrorManager.php");
14 function Template ($name, &$request, $args = false) {
17 $this->_request = &$request;
19 $GLOBALS['TemplatesProcessed'][$name] = 1;
20 $this->_basepage = $request->getArg('pagename');
22 $file = $WikiTheme->findTemplate($name);
24 trigger_error("no template for $name found", E_USER_WARNING);
27 $fp = fopen($file, "rb");
29 trigger_error("$file not found", E_USER_WARNING);
32 $this->_tmpl = fread($fp, filesize($file));
34 //$userid = $request->_user->_userid;
36 $this->_locals = $args;
38 $this->_locals = array('CONTENT' => $args);
40 $this->_locals = array();
43 function _munge_input($template) {
45 // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
46 $orig[] = '/<\?plugin.*?\?>/se';
47 $repl[] = "\$this->_mungePlugin('\\0')";
49 // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
50 $orig[] = '/<\?=(.*?)\?>/s';
51 $repl[] = '<?php $this->_print(\1);?>';
53 return preg_replace($orig, $repl, $template);
56 function _mungePlugin($pi) {
57 // HACK ALERT: PHP's preg_replace, with the /e option seems to
58 // escape both single and double quotes with backslashes.
59 // So we need to unescape the double quotes here...
61 $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
62 return sprintf('<?php $this->_printPlugin(%s); ?>',
63 "'" . str_replace("'", "\'", $pi) . "'");
66 function _printPlugin ($pi) {
67 include_once("lib/WikiPlugin.php");
71 $loader = new WikiPluginLoader;
73 $this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
76 function _print ($val) {
77 if (isa($val, 'Template'))
78 $this->_expandSubtemplate($val);
83 function _expandSubtemplate (&$template) {
85 if (!$template->_request)
86 $template->_request = &$this->_request;
87 if (defined('DEBUG') and DEBUG) {
88 echo "<!-- Begin $template->_name -->\n";
90 // Expand sub-template with defaults from this template.
91 $template->printExpansion($this->_vars);
92 if (defined('DEBUG') and DEBUG) {
93 echo "<!-- End $template->_name -->\n";
98 * Substitute HTML replacement text for tokens in template.
100 * Constructs a new WikiTemplate based upon the named template.
104 * @param $token string Name of token to substitute for.
106 * @param $replacement string Replacement HTML text.
108 function replace($varname, $value) {
109 $this->_locals[$varname] = $value;
113 function printExpansion ($defaults = false) {
114 if (!is_array($defaults))
115 $defaults = array('CONTENT' => $defaults);
116 $this->_vars = array_merge($defaults, $this->_locals);
117 extract($this->_vars);
119 $request = &$this->_request;
121 $user = &$request->getUser();
123 $page = &$request->getPage();
125 global $WikiTheme, $RCS_IDS, $charset;
127 //$this->_dump_template();
129 global $ErrorManager;
130 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
132 eval('?>' . $this->_munge_input($this->_tmpl));
134 $ErrorManager->popErrorHandler();
137 function getExpansion ($defaults = false) {
139 $this->printExpansion($defaults);
140 $xml = ob_get_contents();
145 function printXML () {
146 $this->printExpansion();
150 return $this->getExpansion();
155 function _dump_template () {
156 $lines = explode("\n", $this->_munge_input($this->_tmpl));
159 foreach ($lines as $line)
160 $pre->pushContent(fmt("%4d %s\n", $n++, $line));
164 function _errorHandler($error) {
165 //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
168 if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
169 $error->errfile = "In template '$this->_name'";
170 // Hack alert: Ignore 'undefined variable' messages for variables
171 // whose names are ALL_CAPS.
172 if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
176 $error->errfile .= " (In template '$this->_name')";
178 $lines = explode("\n", $this->_tmpl);
179 if (isset($lines[$error->errline - 1]))
180 $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
188 * This is a convenience function and is equivalent to:
193 function Template($name, $args = false) {
195 return new Template($name, $request, $args);
198 function alreadyTemplateProcessed($name) {
199 return !empty($GLOBALS['TemplatesProcessed'][$name]) ? true : false;
202 * Make and expand the top-level template.
205 * @param $content mixed html content to put into the page
206 * @param $title string page title
207 * @param $page_revision object A WikiDB_PageRevision object
208 * @param $args hash Extract args for top-level template
210 * @return string HTML expansion of template.
212 function GeneratePage($content, $title, $page_revision = false, $args = false) {
215 if (!is_array($args))
218 $args['CONTENT'] = $content;
219 $args['TITLE'] = $title;
220 $args['revision'] = $page_revision;
222 if (!isset($args['HEADER']))
223 $args['HEADER'] = $title;
225 printXML(new Template('html', $request, $args));
230 * For dumping pages as html to a file.
232 function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
235 if (!is_array($args))
238 $content->_basepage = $title;
239 $args['CONTENT'] = $content;
240 $args['TITLE'] = SplitPagename($title);
241 $args['revision'] = $page_revision;
243 if (!isset($args['HEADER']))
244 $args['HEADER'] = SplitPagename($title);
246 global $HIDE_TOOLBARS, $NO_BASEHREF, $HTML_DUMP;
247 $HIDE_TOOLBARS = true;
250 $html = asXML(new Template('htmldump', $request, $args));
252 $HIDE_TOOLBARS = false;
257 // $Log: not supported by cvs2svn $
258 // Revision 1.59 2004/05/18 16:23:39 rurban
259 // rename split_pagename to SplitPagename
261 // Revision 1.58 2004/05/15 19:48:33 rurban
262 // fix some too loose PagePerms for signed, but not authenticated users
263 // (admin, owner, creator)
264 // no double login page header, better login msg.
265 // moved action_pdf to lib/pdf.php
267 // Revision 1.57 2004/05/01 18:20:05 rurban
268 // Add $charset to template locals (instead of constant CHARSET)
270 // Revision 1.56 2004/04/12 13:04:50 rurban
271 // added auth_create: self-registering Db users
273 // removed rating recommendations
274 // ziplib reformatting
276 // Revision 1.55 2004/04/02 15:06:55 rurban
277 // fixed a nasty ADODB_mysql session update bug
278 // improved UserPreferences layout (tabled hints)
279 // fixed UserPreferences auth handling
280 // improved auth stability
281 // improved old cookie handling: fixed deletion of old cookies with paths
283 // Revision 1.54 2004/03/02 18:11:39 rurban
284 // CreateToc support: Pass the preparsed markup to each plugin as $dbi->_markup
285 // to be able to know about its context, and even let the plugin change it.
288 // Revision 1.53 2004/02/22 23:20:31 rurban
289 // fixed DumpHtmlToDir,
290 // enhanced sortby handling in PageList
291 // new button_heading th style (enabled),
292 // added sortby and limit support to the db backends and plugins
293 // for paging support (<<prev, next>> links on long lists)
295 // Revision 1.52 2003/12/20 23:59:19 carstenklapp
296 // Internal change: Added rcs Log tag & emacs php mode tag (sorry, forgot
297 // this in the last commit).
304 // c-hanging-comment-ender-p: nil
305 // indent-tabs-mode: nil