/**
  * Render shortcodes.
  *
  * @param  string     $content The content to render.
  * @param  array      $options Options to be passed to the renderer.
  * @param  null|Page  $page    Null or an instance of \Grav\Common\Page.
  *
  * @return string              The modified contents.
  */
 public function render($content, $options = [], $page = null)
 {
     // Build an anonymous function to pass to twig `render` method
     $function = function ($tag, $body, $arguments) use($options, $page) {
         if (isset($this->shortcodes[$tag])) {
             $options = isset($options[$tag]) ? $options[$tag] : [];
         }
         $event = new Event(['body' => $body, 'options' => new Data(array_replace_recursive($options, $arguments)), 'grav' => self::getGrav(), 'shortcodes' => $this, 'page' => $page, 'tag' => $tag]);
         return $event;
     };
     // Wrapper for shortcodes filter function
     $filter_function = function ($name, $content, $context, $env) {
         return $this->filterShortcode($name, $content, $context, $env);
     };
     // Process in-page shortcodes Twig
     $name = '@Shortcodes:' . $page->path();
     $this->loader->setTemplate($name, $content);
     $vars = ['__shortcodes' => $function, '__shortcodes_filter' => $filter_function];
     try {
         $page_default = $this->page;
         $this->page = $page;
         $output = $this->twig->render($name, $vars);
     } catch (\Twig_Error_Loader $e) {
         throw new \RuntimeException($e->getRawMessage(), 404, $e);
     }
     $shortcodes = isset($page->header()->shortcodes) ? $page->header()->shortcodes : [];
     if (isset($shortcodes['extra'])) {
         /** @var Cache $cache */
         $cache = self::getGrav()['cache'];
         $cache_id = md5('shortcodes' . $page->id() . $cache->getKey());
         $cache->save($cache_id, $shortcodes['extra']);
     }
     $this->page = $page_default;
     return $output;
 }
Пример #2
0
 public function testTemplateReference()
 {
     $name = new Twig_Test_Loader_TemplateReference('foo');
     $loader = new Twig_Loader_Array(array('foo' => 'bar'));
     $loader->getCacheKey($name);
     $loader->getSource($name);
     $loader->isFresh($name, time());
     $loader->setTemplate($name, 'foobar');
 }
Пример #3
0
 /**
  * Adds or overrides a template.
  *
  * @param string $name     The template name
  * @param string $template The template source
  */
 public function setTemplate($name, $template)
 {
     $this->loaderArray->setTemplate($name, $template);
 }
Пример #4
0
 public function testSetTemplate()
 {
     $loader = new Twig_Loader_Array(array());
     $loader->setTemplate('foo', 'bar');
     $this->assertEquals('bar', $loader->getSource('foo'));
 }
Пример #5
0
 /**
  * @return Twig_Environment
  */
 protected function getTwig()
 {
     if (null === $this->twig) {
         $loader = new Twig_Loader_Array(array());
         $loader->setTemplate('nx_object_list', file_get_contents(__DIR__ . '/resources/nx_object_list.twig'));
         $loader->setTemplate('nx_settings', file_get_contents(__DIR__ . '/resources/nx_settings.twig'));
         $this->twig = new Twig_Environment($loader);
         $this->twig->addFilter(new Twig_SimpleFilter('nx_parse_date', array($this, 'parse_date_filter')));
         $this->twig->addFilter(new Twig_SimpleFilter('nx_url', array($this, 'document_url_filter')));
         $this->twig->addFunction(new Twig_SimpleFunction('settings_fields', 'settings_fields'));
         $this->twig->addFunction(new Twig_SimpleFunction('do_settings_sections', 'do_settings_sections'));
         $this->twig->addFunction(new Twig_SimpleFunction('get_option', 'get_option'));
         $this->twig->addFunction(new Twig_SimpleFunction('_e', '_e'));
     }
     return $this->twig;
 }