Ejemplo n.º 1
0
 /**
  * Constructor
  *
  * @param string $tplFile absolute path to the template source
  * @param string $outputFile absolute path to the resulting compiled file
  * @param mixed $template_dir list of directories where to look for the template files
  * @return void
  */
 public function __construct($tplFile, $outputFile, $template_dir = '')
 {
     $this->template_dir = $template_dir != '' ? $template_dir : array(getcwd() . '/templates/');
     $this->addDep($tplFile);
     //gets primary tpl content
     $this->tpl = file_get_contents($tplFile);
     //includes, extends, blocks extractions
     $this->tpl = $this->tplIncludesAndExtends($this->tpl);
     //blocks replacements
     $this->replaceBlocks();
     //tpl logic tags and user's functions, vars
     $this->tpl = tpltools::analyseAndTransform($this->tpl);
     //raw output
     if (isset($_GET['tplraw'])) {
         die($this->tpl);
     }
     //write output
     $output = '<?php' . PHP_EOL . 'return array(' . PHP_EOL . '"deps" => ' . var_export($this->dep, true) . ',' . PHP_EOL . '"tpl" => function($_tpl) { ?>' . PHP_EOL . $this->tpl . PHP_EOL . '<?php }' . ');' . '?>';
     $f = fopen($outputFile, 'w');
     fwrite($f, $output);
     fclose($f);
 }
Ejemplo n.º 2
0
 /**
  * Gets a variable, parses it if necessary, and returns it
  *
  * @param string $var the variable to process
  * @param string $modifiers the modifiers to apply to the variable
  * @param string $returnMethod the return method to use (echo|return)
  * @return mixed a string if returnMethod=='echo', void else
  * @see self::modifier()
  */
 public static function getVar($var, $modifiers = '', $returnMethod = 'return')
 {
     $parse = isset($var['_parse_']) && $var['_parse_'];
     $var = is_array($var) && array_key_exists('_value_', $var) ? $var['_value_'] : $var;
     $modifiers = explode('|', $modifiers);
     foreach ($modifiers as $modifier) {
         $t = explode(':', $modifier);
         $var = tpltools::modifier($var, $t[0], isset($t[1]) ? $t[1] : '');
     }
     if (is_string($var) && $parse) {
         $var = eval('?>' . tpltools::analyseAndTransform($var));
     }
     if ($returnMethod == 'echo') {
         echo $var;
     } elseif ($returnMethod == 'return') {
         return $var;
     }
 }