Browse Source

Begin working on allowing data to be passed to widgets

Nathaniel van Diepen 6 years ago
parent
commit
853a0baf0d
1 changed files with 16 additions and 2 deletions
  1. 16 2
      Data/template.class.php

+ 16 - 2
Data/template.class.php

@@ -45,7 +45,7 @@
 			'eval'=>'/\{\?([\W\w\S\s]+)\?\}/i',
 			'include'=>'/{#include ([^}]+)}/i',
 			'define'=>'/\{#define ([^}]*)\}([\S\s]*)\{\/define \1\}/i',
-			'widget'=>'/{#widget ([^}]+)}/i'
+			'widget'=>'/{#widget ([^ ]+)(:? ([^=]+=[^}]+,?)*)?}/i'
 		];
 		protected static $parsers;
 		private $template;
@@ -94,7 +94,21 @@
 								if(!isset($widgets[$name])){
 									throw new \Exception("Widget {$name} is not defined");
 								}
-								return $widgets[$name];
+								if(count($matches) > 2){
+									$args = [];
+									foreach(explode('&', $matches[2]) as $chunk){
+										$param = explode('=', $chunk);
+										if($param){
+											$args[urldecode($param[0])] = urldecode($param[1]);
+										}
+									}
+									$widget = "<?php \$widget_parent[] = \$data; \$data = array_merge(\$data, unserialize(".json_encode(serialize($args)).")); ?>";
+									$widget .= static::compile($widgets[$name]);
+									$widget .= "<?php \$data = array_pop(\$widget_parent); ?>";
+								}else{
+									$widget = $widgets[$name];
+								}
+								return $widget;
 							}, $output);
 						}
 					},