Ejemplo n.º 1
0
 /**
  * Render a given token.
  *
  * Attempts to render a token. If the view is non-scalar, the token is not
  * one it handles, or the variable does not match the iterator name, it
  * returns null, returning control to the renderer.
  *
  * Otherwise, it will output the view, escaping it unless the token
  * indicates a raw value.
  *
  * @param  array $tokenStruct
  * @param  mixed $view
  * @param  array $options
  * @return mixed
  */
 public function render(array $tokenStruct, $view, array $options, Mustache $mustache)
 {
     // If we don't have a scalar view, implicit iteration isn't possible
     if (!is_scalar($view)) {
         return;
     }
     // Do we escape?
     $escape = true;
     switch ($tokenStruct[0]) {
         case Lexer::TOKEN_VARIABLE:
             // Yes
             break;
         case Lexer::TOKEN_VARIABLE_RAW:
             // No
             $escape = false;
             break;
         default:
             // Wrong token type! Just return;
             return;
     }
     // Get the iterator option, and compare it to the token we received
     $iterator = isset($options['iterator']) ? $options['iterator'] : '.';
     if ($iterator !== $tokenStruct[1]) {
         return;
     }
     // Match found, so replace the value
     return $escape ? $mustache->getRenderer()->escape($view) : $view;
 }
Ejemplo n.º 2
0
 /**
  * Render a given token.
  *
  * If the token is not a variable, does not contain contextual
  * information, returns null.
  *
  * If the view is scalar, escapes it using the context.
  *
  * If the view is not scalar, checks for the value in the view; if not
  * present, returns null; otherwise, escapes the view value.
  *
  * @param  array $tokenStruct
  * @param  mixed $view
  * @param  array $options
  * @param  Mustache $mustache Mustache instance handling rendering.
  * @return mixed
  */
 public function render(array $tokenStruct, $view, array $options, Mustache $mustache)
 {
     if ($tokenStruct[0] !== Lexer::TOKEN_VARIABLE) {
         return null;
     }
     if (!isset($tokenStruct[2]) || !in_array($tokenStruct[2], $this->validContexts, true)) {
         return null;
     }
     if (is_scalar($view)) {
         return $mustache->getRenderer()->escape($view, $tokenStruct[2]);
     }
     if (is_array($view) && isset($view[$tokenStruct[1]])) {
         $value = $view[$tokenStruct[1]];
     } elseif (is_object($view) && isset($view->{$tokenStruct[1]})) {
         $value = $view->{$tokenStruct[1]};
     } elseif (is_object($view) && method_exists($view, $tokenStruct[1])) {
         $value = $view->{$tokenStruct[1]}();
     } else {
         return null;
     }
     if (is_callable($value)) {
         $value = $value();
     }
     return $mustache->getRenderer()->escape($value, $tokenStruct[2]);
 }
Ejemplo n.º 3
0
 /**
  * Return a configured instance of Mustache
  *
  * Important: the page class uses its own loading mechanism instead of Mustache's.
  *
  * @return Phly\Mustache\Mustache
  */
 static function getMustache()
 {
     if (null === static::$mustache) {
         $mustache = new Mustache();
         $mustache->getRenderer()->addPragma(new \Phly\Mustache\Pragma\ImplicitIterator())->addPragma(new \Plugin\Pages\Pragma\FormatDate());
         static::$mustache = $mustache;
     }
     return static::$mustache;
 }
 /**
  * Inject the renderer, if needed, and potentially the escaper.
  *
  * @param array $config
  * @param Mustache $mustache
  * @param ContainerInterface $container
  */
 private function injectRenderer(array $config, Mustache $mustache, ContainerInterface $container)
 {
     if (isset($config['renderer'])) {
         if (is_string($config['renderer']) && $container->has($config['renderer'])) {
             // Assume fully configured at this point.
             $mustache->setRenderer($container->get($config['renderer']));
             return;
         }
         if ($config['renderer'] instanceof Renderer) {
             $mustache->setRenderer($config['renderer']);
         }
         if (is_string($config['renderer']) && class_exists($config['renderer'])) {
             $mustache->setRenderer(new $config['renderer']());
         }
     }
     if (!isset($config['escaper'])) {
         return;
     }
     if ($config['escaper'] instanceof Escaper) {
         $mustache->getRenderer()->setEscaper($config['escaper']);
         return;
     }
     if (!is_string($config['escaper'])) {
         return;
     }
     if ($container->has($config['escaper'])) {
         $mustache->getRenderer()->setEscaper($container->get($config['escaper']));
         return;
     }
     if (class_exist($config['escaper'])) {
         $mustache->getRenderer()->setEscaper(new $config['escaper']());
         return;
     }
 }
Ejemplo n.º 5
0
 /**
  * Set manager object
  *
  * Sets manager object and registers self as a pragma on the renderer.
  *
  * @param  Mustache $manager
  * @return SubViews
  */
 public function setManager(Mustache $manager)
 {
     $this->manager = $manager;
     $this->manager->getRenderer()->addPragma($this);
     return $this;
 }