/**
  * @param $filename_template
  * @param array $data
  * @return string
  */
 public function render($filename_template, array $data = [])
 {
     if (false === strstr($filename_template, '{{')) {
         return $filename_template;
     }
     $data = ArrayUtils::merge($data, $this->mixins);
     return $this->mustacheEngine->render($filename_template, $data);
 }
Example #2
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;
 }
Example #3
0
 /**
  * Register a pragma for the current rendering session
  *
  * @param  array $definition
  */
 private function registerPragma(array $definition)
 {
     $pragmas = $this->mustache->getPragmas();
     $name = $definition['pragma'];
     if (!$pragmas->has($name)) {
         throw new Exception\UnregisteredPragmaException(sprintf('No handler for pragma "%s" registered; cannot proceed rendering', $name));
     }
     $this->invokedPragmas[$name] = $definition['options'];
 }
Example #4
0
 /**
  * Inits the route for page rendering, registers a layout renderer and 
  * provides fancy error pages
  *
  * @return void
  */
 function init()
 {
     $routes = $this->import("Routes");
     $app = $this->import("App");
     $config = $this->import("Config");
     $routes->addRoute(new PageRoute(array($this, "render")));
     Page::setSearchPath(array($this->getPath() . "/default", \Core\APPROOT . "/pages"));
     // An object which holds the layout variables
     $layout = new StdClass();
     $this->export("Layout", $layout);
     $layout->title = "Willkommen zu SimpleCMS!";
     $layoutRenderer = new Mustache();
     $layoutRenderer->setTemplatePath($this->getPath() . "/default")->setTemplatePath(\Core\APPROOT . "/layouts");
     $layoutName = isset($config["Pages"]["layout_name"]) ? $config["Pages"]["layout_name"] : "layout";
     // Render the layout after the dispatching process
     $app->after(function ($request, $response) use($layoutName, $layout, $layoutRenderer) {
         $body = $response->getBody();
         $layout->content = $body;
         $response->setBody($layoutRenderer->render($layoutName, $layout));
     });
     // Checks if the response has errors and renders appropiate error pages
     $app->error(new ErrorHandler());
 }
 /**
  * {@inheritDoc}
  */
 public function render($name, $vars = [])
 {
     $vars = $this->mergeParams($name, $vars);
     return $this->renderer->render($name, $vars);
 }
Example #6
0
 /**
  * Render a template
  *
  * Proxies to parent object, but provides defaults for $viewModel and
  * $partials.
  *
  * @param  string $template  Either a template string or a template file in the template path
  * @param  mixed  $viewModel An array or object with items to inject in the template
  * @param  mixed  $partials  A list of partial names/template pairs for rendering as partials
  * @return string
  */
 public function render($template, $viewModel = [], $partials = null)
 {
     return parent::render($template, $viewModel, $partials);
 }
 /**
  * @group issue-25
  */
 public function testSubLayoutsCanAlterContentOfParent()
 {
     $mustache = new Mustache();
     $mustache->setTemplatePath(__DIR__ . '/templates/no-layout-dups');
     $view = new stdClass();
     $view->name = 'Stan';
     $layout = $mustache->render('layout', $view);
     $this->assertContains('Hello Stan', $layout);
     $this->assertContains('Default content of the page', $layout);
     $subLayout = $mustache->render('sub-layout', $view);
     $this->assertContains('Salutations Stan', $subLayout);
     $this->assertContains('Lorem ipsum, yada yada yada...', $subLayout);
 }
Example #8
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]);
 }
Example #9
0
 /**
  * Render a sub view variable.
  *
  * If the data/view combination do not represent a subview, it returns null,
  * returning handling to the renderer.
  *
  * Otherwise, it will render the template and view in the SubView provided.
  *
  * @param  array $tokenStruct
  * @param  mixed $view
  * @param  array $options
  * @param  Mustache $mustache
  * @return mixed
  */
 public function render(array $tokenStruct, $view, array $options, Mustache $mustache)
 {
     $subView = $this->getValue($tokenStruct[1], $view);
     // If the view value is not a SubView, we cannot handle it here
     if (!$subView instanceof SubView) {
         return;
     }
     // Get template
     $template = $subView->getTemplate();
     // Get sub view; use current view if none found
     $localView = $subView->getView();
     if (null === $localView) {
         $localView = $view;
     }
     // Render sub view and return it
     return $mustache->render($template, $localView);
 }
 /**
  * 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;
     }
 }
Example #11
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;
 }
Example #12
0
 /**
  * Send the directory listing.
  */
 public function sendListing()
 {
     $app = Application::getInstance();
     $request = $app->getRequest();
     // Build up data:
     $data = (object) ['resource' => ['dir' => $request->getResourcesDir(), 'uri' => $request->getResourcesUri()], 'request' => ['dir' => $request->getRequestDir(), 'uri' => $request->getRequestUri()], 'features' => ['archive' => $this->archiveDownload], 'breadcrumb' => $app->getBreadcrumb(), 'readme' => [], 'results' => []];
     foreach ($app->getReadme() as $item) {
         $document = new DOMDocument('1.0', 'utf-8');
         $document->loadHTML('<?xml encoding="UTF-8">' . $item->html);
         $xpath = new DOMXPath($document);
         // Find any 'neatindex-action' links:
         foreach ($xpath->query('//link[@rel = "neatindex-action"]') as $node) {
             // Remove the node from the document:
             $node->parentNode->removeChild($node);
             if (isset($item->actions) === false) {
                 $item->actions = [];
             }
             $item->actions[] = (object) ['name' => $node->getAttribute('title'), 'url' => $node->getAttribute('href')];
         }
         // Rebuild the HTML:
         $item->html = null;
         foreach ($xpath->query('//body/node()') as $node) {
             $item->html .= $document->saveHTML($node);
         }
         $data->readme[] = $item;
     }
     foreach ($app->getResults() as $item) {
         // Set locale timestamp:
         $item->date->value = strftime($this->dateFormat, $item->date->timestamp);
         // Give files a class based on their extension:
         if ($item->type->file) {
             $classes = [];
             // Give files a class based on their mime:
             if (isset($item->type->mime)) {
                 $classes = explode('/', $item->type->mime);
             }
             if (preg_match('%.[.].%', $item->name)) {
                 $classes[] = preg_replace('%(^.+?)([.]([^.]+$))%', 'extension-\\3', strtolower($item->name));
             }
             $item->type->class = implode(' ', $classes);
         }
         $data->results[] = $item;
     }
     // Render output:
     $mustache = new Mustache();
     $mustache->setSuffix('html');
     $mustache->setTemplatePath($request->getResourcesDir() . '/themes/adwaita/templates');
     header('content-type: text/html;charset=utf-8');
     echo $mustache->render('main', $data);
 }
Example #13
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;
 }
 /**
  * @test
  */
 public function it_resolves_data_before_rendering_current_date()
 {
     $this->assertEquals(date('d.m.Y'), $this->mustache->render('{{#now}}{{data.format}}{{/now}}', ['now' => $this->mixin, 'data' => ['format' => 'd.m.Y']]));
 }