Exemplo n.º 1
0
 function parse()
 {
     foreach ($this->files as $file) {
         $flexy = new HTML_Template_Flexy(array('compileToString' => true, 'valid_functions' => 'include'));
         $compiler = HTML_Template_Flexy_Compiler::factory($flexy->options);
         $result = $compiler->compile($flexy, file_get_contents($file));
         echo $result;
         print_r(array_unique($GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN']['gettextStrings']));
         print_r($flexy->elements);
     }
 }
Exemplo n.º 2
0
 /**
  *   compile the template
  *
  *   @access     public
  *   @version    01/12/03
  *   @author     Wolfram Kriesing <*****@*****.**>
  *   @param      string  $file   relative to the 'templateDir' which you set when calling the constructor
  *   @return     boolean true on success. (or string, if compileToString) PEAR_Error on failure..
  */
 function compile($file)
 {
     if (!$file) {
         return $this->raiseError('HTML_Template_Flexy::compile no file selected', HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS, HTML_TEMPLATE_FLEXY_ERROR_DIE);
     }
     if (!@$this->options['locale']) {
         $this->options['locale'] = 'en';
     }
     //Remove the slash if there is one in front, just to be safe.
     $file = ltrim($file, DIRECTORY_SEPARATOR);
     if (strpos($file, '#')) {
         list($file, $this->options['output.block']) = explode('#', $file);
     }
     $parts = array();
     $tmplDirUsed = false;
     // PART A mulitlanguage support: ( part B is gettext support in the engine..)
     //    - user created language version of template.
     //    - compile('abcdef.html') will check for compile('abcdef.en.html')
     //       (eg. when locale=en)
     $this->currentTemplate = false;
     if (preg_match('/(.*)(\\.[a-z]+)$/i', $file, $parts)) {
         $newfile = $parts[1] . '.' . $this->options['locale'] . $parts[2];
         foreach ($this->options['templateDir'] as $tmplDir) {
             if (@(!file_exists($tmplDir . DIRECTORY_SEPARATOR . $newfile))) {
                 continue;
             }
             $file = $newfile;
             $this->currentTemplate = $tmplDir . DIRECTORY_SEPARATOR . $newfile;
             $tmplDirUsed = $tmplDir;
         }
     }
     // look in all the posible locations for the template directory..
     if ($this->currentTemplate === false) {
         $dirs = array_unique($this->options['templateDir']);
         if ($this->options['templateDirOrder'] == 'reverse') {
             $dirs = array_reverse($dirs);
         }
         foreach ($dirs as $tmplDir) {
             if (!@file_exists($tmplDir . DIRECTORY_SEPARATOR . $file)) {
                 continue;
             }
             if (!$this->options['multiSource'] && $this->currentTemplate !== false) {
                 return $this->raiseError("You have more than one template Named {$file} in your paths, found in both" . "<BR>{$this->currentTemplate}<BR>{$tmplDir}" . DIRECTORY_SEPARATOR . $file, HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS, HTML_TEMPLATE_FLEXY_ERROR_DIE);
             }
             $this->currentTemplate = $tmplDir . DIRECTORY_SEPARATOR . $file;
             $tmplDirUsed = $tmplDir;
         }
     }
     if ($this->currentTemplate === false) {
         // check if the compile dir has been created
         return $this->raiseError("Could not find Template {$file} in any of the directories<br>" . implode("<BR>", $this->options['templateDir']), HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS, HTML_TEMPLATE_FLEXY_ERROR_DIE);
     }
     // Savant compatible compiler
     if (is_string($this->options['compiler']) && $this->options['compiler'] == 'Raw') {
         $this->compiledTemplate = $this->currentTemplate;
         $this->debug("Using Raw Compiler");
         return true;
     }
     // now for the compile target
     //If you are working with mulitple source folders and $options['multiSource'] is set
     //the template folder will be:
     // compiled_tempaltes/{templatedir_basename}_{md5_of_dir}/
     $compileSuffix = count($this->options['templateDir']) > 1 && $this->options['multiSource'] ? DIRECTORY_SEPARATOR . basename($tmplDirUsed) . '_' . md5($tmplDirUsed) : '';
     $compileDest = isset($this->options['compileDir']) ? $this->options['compileDir'] : '';
     $isTmp = false;
     // Use a default compile directory if one has not been set.
     if (!$compileDest) {
         // Use session.save_path + 'compiled_templates_' + md5(of sourcedir)
         $compileDest = ini_get('session.save_path') . DIRECTORY_SEPARATOR . 'flexy_compiled_templates';
         if (!file_exists($compileDest)) {
             require_once 'System.php';
             System::mkdir(array('-p', $compileDest));
         }
         $isTmp = true;
     }
     // we generally just keep the directory structure as the application uses it,
     // so we dont get into conflict with names
     // if we have multi sources we do md5 the basedir..
     $base = $compileDest . $compileSuffix . DIRECTORY_SEPARATOR . $file;
     $fullFile = $this->compiledTemplate = $base . '.' . $this->options['locale'] . '.php';
     $this->getTextStringsFile = $base . '.gettext.serial';
     $this->elementsFile = $base . '.elements.serial';
     if (isset($this->options['output.block'])) {
         $this->compiledTemplate .= '#' . $this->options['output.block'];
     }
     if (!empty($this->options['dontCompile'])) {
         return true;
     }
     $recompile = false;
     $isuptodate = file_exists($this->compiledTemplate) ? filemtime($this->currentTemplate) == filemtime($this->compiledTemplate) : 0;
     if (!empty($this->options['forceCompile']) || !$isuptodate) {
         $recompile = true;
     } else {
         $this->debug("File looks like it is uptodate.");
         return true;
     }
     if (!@is_dir($compileDest) || !is_writeable($compileDest)) {
         require_once 'System.php';
         System::mkdir(array('-p', $compileDest));
     }
     if (!@is_dir($compileDest) || !is_writeable($compileDest)) {
         return $this->raiseError("can not write to 'compileDir', which is <b>'{$compileDest}'</b><br>" . "Please give write and enter-rights to it", HTML_TEMPLATE_FLEXY_ERROR_FILE, HTML_TEMPLATE_FLEXY_ERROR_DIE);
     }
     if (!file_exists(dirname($this->compiledTemplate))) {
         require_once 'System.php';
         System::mkdir(array('-p', '-m', 0770, dirname($this->compiledTemplate)));
     }
     // Compile the template in $file.
     require_once 'HTML/Template/Flexy/Compiler.php';
     $compiler = HTML_Template_Flexy_Compiler::factory($this->options);
     $ret = $compiler->compile($this);
     if (HTML_Template_Flexy_is_a($ret, 'PEAR_Error')) {
         return $this->raiseError('HTML_Template_Flexy fatal error:' . $ret->message, $ret->code, HTML_TEMPLATE_FLEXY_ERROR_DIE);
     }
     return $ret;
     //return $this->$method();
 }