Added template context API and render_getpage_norender hook. This allows variable sets to be pushed and popped from $template, so that variables in a template inclusion can be accessed and pre-processed by plugins.
authorDan Fuhry <dan@enanocms.org>
Sat, 15 Sep 2012 13:18:01 -0400
changeset 1363 f1ed3e9298ae
parent 1362 c690f0b39bcb
child 1364 68c17d935750
Added template context API and render_getpage_norender hook. This allows variable sets to be pushed and popped from $template, so that variables in a template inclusion can be accessed and pre-processed by plugins.
includes/render.php
includes/template.php
includes/wikiengine/parse_mediawiki.php
--- a/includes/render.php	Sat Sep 15 13:16:13 2012 -0400
+++ b/includes/render.php	Sat Sep 15 13:18:01 2012 -0400
@@ -45,7 +45,14 @@
 		$text = $page->fetch_text();
 		
 		if ( !$render )
+		{
+			$code = $plugins->setHook('render_getpage_norender');
+			foreach ( $code as $cmd )
+			{
+				eval($cmd);
+			}
 			return $text;
+		}
 		
 		$text = self::render($text, $wiki, $smilies, $filter_links);
 		return $text;
@@ -54,10 +61,14 @@
 	public static function getTemplate($id, $parms)
 	{
 		global $db, $session, $paths, $template, $plugins; // Common objects
+		
+		// Verify target exists -- if not, double-bracket it to convert it to a redlink
 		if ( !isPage($paths->get_pathskey($id, 'Template')) ) 
 		{
 			return '[['.$paths->nslist['Template'].$id.']]';
 		}
+		
+		// fetch from DB or template cache
 		if(isset($paths->template_cache[$id]))
 		{
 			$text = $paths->template_cache[$id];
@@ -69,6 +80,8 @@
 			$paths->template_cache[$id] = $text;
 		}
 		
+		// noinclude is not shown within the included template, only on the template's page when you load it
+		// nodisplay is not shown on the template's page, only in the included template
 		$text = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '', $text);
 		$text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '\\1', $text);
 		
@@ -91,7 +104,7 @@
 		return $text;
 	}
 	
-	public static function fetch_template_text($id)
+	public static function fetch_template_text($id, $params)
 	{
 		global $db, $session, $paths, $template, $plugins; // Common objects
 		$fetch_ns = 'Template';
@@ -131,6 +144,11 @@
 				return '[['.$paths->nslist['Template'].$id.']]';
 			}
 		}
+		
+		$template->context_push();
+		
+		$template->assign_vars($params);
+		
 		if(isset($paths->template_cache[$id]))
 		{
 			$text = $paths->template_cache[$id];
@@ -141,6 +159,8 @@
 			$paths->template_cache[$id] = $text;
 		}
 		
+		$template->context_pop();
+		
 		if ( is_string($text) )
 		{
 			$text = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '', $text);
@@ -894,7 +914,7 @@
 				{
 					$parms = Array();
 				}
-				if ( $tpl_code = RenderMan::fetch_template_text($matches[1][$i]) )
+				if ( $tpl_code = RenderMan::fetch_template_text($matches[1][$i], $parms) )
 				{
 					// Intelligent paragraphs.
 					// If:
--- a/includes/template.php	Sat Sep 15 13:16:13 2012 -0400
+++ b/includes/template.php	Sat Sep 15 13:18:01 2012 -0400
@@ -60,6 +60,13 @@
 	
 	var $fading_button = '';
 	
+	/**
+	* Context stack. You can save and restore the var set.
+	* @var array
+	*/
+
+	var $context_stack = array('bool' => array(), 'str' => array(), 'history' => array());
+	
 	function __construct()
 	{
 		global $db, $session, $paths, $template, $plugins; // Common objects
@@ -119,6 +126,24 @@
 	}
 	
 	/**
+	* Save the current context and start a blank one.
+	*/
+	
+	function context_push()
+	{
+		array_push($this->context_stack['str'], $this->tpl_strings);
+		array_push($this->context_stack['bool'], $this->tpl_bool);
+		array_push($this->context_stack['history'], $this->vars_assign_history);
+	}
+	
+	function context_pop()
+	{
+		$this->tpl_strings = array_pop($this->context_stack['str']);
+		$this->tpl_bool = array_pop($this->context_stack['bool']);
+		$this->vars_assign_history = array_pop($this->context_stack['history']);
+	}
+	
+	/**
  	* Gets the list of available CSS files (styles) for the specified theme.
  	* @param string Theme ID
  	* @return array
--- a/includes/wikiengine/parse_mediawiki.php	Sat Sep 15 13:16:13 2012 -0400
+++ b/includes/wikiengine/parse_mediawiki.php	Sat Sep 15 13:18:01 2012 -0400
@@ -54,11 +54,15 @@
 		while ( preg_match($template_regex, $text, $match) )
 		{
 			$i++;
+			// we can go up to 5 levels of templates deep
 			if ( $i == 5 )
 				break;
 			$text = RenderMan::include_templates($text);
 		}
 		
+		//header('Content-type: text/plain');
+		//die($text);
+		
 		return array();
 	}