. */ // This is a simple example of a compiler (generates PHP code from templates), like in smarty require '../class.miniskel.php'; class skelCompiler extends miniSkel { private function buildArray($values) { if (empty($values)) return 'array()'; $out = 'array('; foreach ($values as $key=>$value) { if (is_object($value)) continue; $out .= (is_int($key) ? $key : '"'.addslashes($key).'"') . ' => '; if (is_array($value)) $out .= $this->buildArray($value); elseif (is_bool($value)) $out .= $value ? 'true' : 'false'; elseif (is_int($value)) $out .= $value; else $out .= '"'.addslashes($value).'"'; $out .= ", "; } $out .= ')'; return $out; } protected function processLoop($loopName, $loopType, $loopCriterias, $loopContent, $preContent, $postContent, $altContent) { $method_name = 'processLoopType_' . $loopType; $out = "\nbuildArray($loopCriterias).";\n" . '$loopElements = $skelMethods->'.$method_name.'($loopCriterias);' . "\n\n" . 'if (!empty($loopElements)): ?>'; if ($preContent) { $out .= $this->parse($preContent, $loopName, miniSkel::PRE_CONTENT); } $out .= "\n" . '' . "\n"; $loopContent = $this->parseVariables($loopContent, array(), miniSkel::CONTEXT_IN_LOOP); $out .= $this->parse($loopContent, $loopName, miniSkel::LOOP_CONTENT); $out .= "\n" . '' . "\n"; if ($postContent) { $out .= $this->parse($postContent, $loopName, self::POST_CONTENT); } if ($altContent) { $out .= "\n" . '' . "\n"; $out .= $this->parse($altContent, $loopName, self::ALT_CONTENT); } $out .= "\n" . '' . "\n"; return $out; } protected function processVariable($name, $value, $applyDefault, $modifiers, $pre, $post, $context) { if ($context == miniSkel::CONTEXT_GLOBAL) $var = '$variables["'.$name.'"]'; else $var = '$row["'.$name.'"]'; $out = "\n' . "\n"; if ($pre) $out .= $this->parseVariables($pre, false, $context); $out .= 'callModifier("default", '.$var.')'; } foreach ($modifiers as &$modifier) { $var = '$skelMethods->callModifier("'.$modifier['name'].'", '.$var.', '.$this->buildArray($modifier['args']).')'; } $out .= $var."; ?>\n"; if ($post) $out .= $this->parseVariables($post, false, $context); $out .= "\n\n"; return $out; } public function fetch($template) { $content = '' . "\n"; $content .= parent::fetch($template); return $content; } } $skel = new skelCompiler; // Generate the code $code = $skel->fetch('compiler_example.tpl'); // run it eval('?>'.$code); ?>