Esempio n. 1
0
 /**
  * Compile handlebars template into PHP code.
  *
  * @param string $template handlebars template string
  * @param array $options LightnCandy compile time and run time options, default is Array('flags' => LightnCandy::FLAG_BESTPERFORMANCE)
  *
  * @return string Compiled PHP code when successed. If error happened and compile failed, return false.
  *
  * @codeCoverageIgnore
  */
 public static function compile($template, $options = array('flags' => self::FLAG_BESTPERFORMANCE))
 {
     $context = self::buildContext($options);
     // Scan for partial and replace partial with template.
     $template = self::expandPartial($template, $context);
     if (self::handleError($context)) {
         return false;
     }
     // Strip extended comments
     $template = preg_replace(self::EXTENDED_COMMENT_SEARCH, '', $template);
     // Do first time scan to find out used feature, detect template error.
     if (preg_match_all(self::TOKEN_SEARCH, $template, $tokens, PREG_SET_ORDER) > 0) {
         foreach ($tokens as $token) {
             self::scanFeatures($token, $context);
         }
     }
     if (self::handleError($context)) {
         return false;
     }
     // Do PHP code generation.
     $code = preg_replace_callback(self::TOKEN_SEARCH, function ($matches) use(&$context) {
         $tmpl = LightnCandy::compileToken($matches, $context);
         return "{$matches[LightnCandy::POS_LSPACE]}'{$tmpl}'{$matches[LightnCandy::POS_RSPACE]}";
     }, addcslashes($template, "'"));
     // return false when fatal error
     if (self::handleError($context)) {
         return false;
     }
     // Or, return full PHP render codes as string
     return self::composePHPRender($context, $code);
 }