LightnCandy\LightnCandy::compile PHP Метод

compile() публичный статический Метод

Compile handlebars template into PHP code.
public static compile ( string $template, array\arraystring | integer> $options = ['flags' => self::FLAG_BESTPERFORMANCE] ) : string | false
$template string handlebars template string
$options array\arraystring | integer>
Результат string | false Compiled PHP code when successed. If error happened and compile failed, return false.
    public static function compile($template, $options = array('flags' => self::FLAG_BESTPERFORMANCE))
    {
        $context = Context::create($options);
        if (static::handleError($context)) {
            return false;
        }
        $code = Compiler::compileTemplate($context, SafeString::escapeTemplate($template));
        static::$lastParsed = Compiler::$lastParsed;
        // return false when fatal error
        if (static::handleError($context)) {
            return false;
        }
        // Or, return full PHP render codes as string
        return Compiler::composePHPRender($context, $code);
    }

Usage Example

Пример #1
0
 /**
  *
  */
 public function renderView($viewCode, $viewContext = null, $viewRoot = null)
 {
     // Fix the view root
     if (empty($viewRoot) === true) {
         $viewRoot = PATH_APP . '/Views';
     }
     // Check whether the view exists
     $pathToView = $viewRoot . '/' . $viewCode;
     if (file_exists($pathToView) === false) {
         \z\e(EXCEPTION_VIEW_NOT_FOUND, ['pathToView' => $pathToView, 'viewCode' => $viewCode, 'viewContext' => $viewContext]);
     }
     // Load the view
     $view = file_get_contents($pathToView);
     // Compile the view
     $code = \LightnCandy\LightnCandy::compile($view, ['basedir' => [PATH_APP . '/Views'], 'fileext' => [''], 'flags' => \LightnCandy\LightnCandy::FLAG_ERROR_EXCEPTION | \LightnCandy\LightnCandy::FLAG_HANDLEBARS | \LightnCandy\LightnCandy::FLAG_RENDER_DEBUG | \LightnCandy\LightnCandy::FLAG_RUNTIMEPARTIAL | \LightnCandy\LightnCandy::FLAG_THIS, 'helpers' => ['locale' => function () {
         return \z\service('manager/culture')->localeCode;
     }, 'str' => function ($indexed, $associative) {
         // Build arguments
         $arguments = $this->buildArguments(['stringCode'], $indexed, $associative);
         // Get the string
         $result = \z\str($arguments['stringCode']);
         return $result;
     }]]);
     // Build path to code
     $pathToCode = '/tmp/' . sha1('fbenard/zero_' . $pathToView) . '.php';
     // Get the view renderer
     file_put_contents($pathToCode, $code);
     $renderer = (require $pathToCode);
     // Render the view
     $result = $renderer($viewContext);
     return $result;
 }
All Usage Examples Of LightnCandy\LightnCandy::compile