/**
  * @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);
 }
Esempio n. 2
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);
 }
Esempio n. 4
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);
 }
Esempio n. 6
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);
 }
Esempio n. 7
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);
 }
 /**
  * @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']]));
 }