* @version 0.1.1 */ class CrioTemplate { private $variables = array(); private $template = null; /** * Load a template from file. * * @param string $file * @return void */ public function loadTemplate($file){ $load = file($file); if(is_array($load)){ $this->template = implode("\n",$load); return true; }else{ return false; } } /** * Load template from a string. * * @param string $source */ public function setTemplate($source){ $this->template = $source; } /** * Clear variables from the template. * */ public function clearVariables(){ $this->variables = array(); } /** * Set variable. * * @param string $name * @param string $value */ public function addVariable($name, $value){ $this->variables[$name] = $value; } /** * Parse the template, and return the formatted source. * * @return string */ public function parseTemplate(){ if(isset($this->template)){ $template = $this->template; if(preg_match_all("/.*?\[if.*?((?:[a-z][a-z0-9_ ]*))\](.*?)\[else\](.*?)\[\/if\]/is", $template, $matches)){ $tagMatches = $matches[0]; $tagAttributes = $matches[1]; $tagTrueContents = $matches[2]; $tagFalseContents = $matches[3]; for($i = 0; $i < sizeof($tagMatches); $i++) $template = str_replace($tagMatches[$i], $this->parseCondition($tagAttributes[$i])?$tagTrueContents[$i]:(isset($tagFalseContents[$i])?$tagFalseContents[$i]:""), $template); } if(preg_match_all("/.*?({((?:[a-z][a-z0-9_]*)),?((?:[a-z0-9_]*)),?((?:[a-z0-9_]*))?}).*?/is", $template, $matches)){ $varMatches = $matches[1]; $varVariables = $matches[2]; $varFunction = $matches[3]; $varParam = $matches[4]; $i = 0; while($i < sizeof($varMatches)){ $template = $this->parseTag($template, $varMatches[$i], $varVariables[$i], $varFunction[$i], $varParam[$i]); $i++; } } return $template; }else{ return false; } } /** * Parse and check condition supported by the template tags. * * @param string $attrs * @return bool */ private function parseCondition($attrs){ $attributes = explode(" " ,$attrs); foreach($attributes as $attribute){ $attribute = trim($attribute); if(substr($attribute, 0, 1) == "!"){ $attr = substr($attribute, 1, strlen($attribute)-1); if(isset($this->variables[$attr]) == false){ continue; }else{ return false; } }else{ if(isset($this->variables[$attribute])){ continue; }else{ return false; } } } return true; } /** * Parse tag from the template. * * @param string $template * @param string $replace * @param string $tag * @param string $func * @param string $param */ private function parseTag($template, $replace, $var, $func, $param){ if(!$func) return str_replace($replace, $this->variables[$var], $template); switch($func){ case 'pad': // Parameter is larger than variable lenght, or is smaller than zero. if($param < 0 || $param >= strlen($this->variables[$var])) return str_replace($replace, $this->variables[$var], $template); // Pad variable content to the given lenght. preg_match("/.{".intval($param)."}/", $this->variables[$var], $ret); return str_replace($replace, $ret[0], $template); break; case 'len': return str_replace($replace, strlen($this->variables[$var]),$template); break; } } /** * Create a template. * * @param string $file * @param array $var * @param array $val */ public function __construct($file = null, $variables = null){ if($file){ $load = file($file); if(is_array($load)){ $this->template = implode("\n", $load); } } if(is_array($variables)){ $this->variables = $variables; } } } define("TEST", true); $time = microtime(true); if(defined("TEST")){ $x = new CrioTemplate(); $x->setTemplate("Name: {FIRST_NAME} {LAST_NAME} {FIRST_NAME,len}{LAST_NAME,pad,3}"); $x->addVariable("FIRST_NAME", "Milen"); $x->addVariable("LAST_NAME", "Ivanov"); echo $x->parseTemplate(); } echo "\n". (microtime(true)-$time); ?>