2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Twig;
|
|
|
|
|
|
|
|
use Block;
|
2015-01-21 21:38:42 -08:00
|
|
|
use Event;
|
2019-03-27 13:15:17 -06:00
|
|
|
use Twig\Extension\AbstractExtension as TwigExtension;
|
|
|
|
use Twig\TwigFilter as TwigSimpleFilter;
|
|
|
|
use Twig\TwigFunction as TwigSimpleFunction;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Cms\Classes\Controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The CMS Twig extension class implements the basic CMS Twig functions and filters.
|
|
|
|
*
|
2021-03-10 15:02:53 -06:00
|
|
|
* @package winter\wn-cms-module
|
2014-05-14 23:24:20 +10:00
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2019-03-27 13:15:17 -06:00
|
|
|
class Extension extends TwigExtension
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Cms\Classes\Controller A reference to the CMS controller.
|
|
|
|
*/
|
2014-08-01 18:20:55 +10:00
|
|
|
protected $controller;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the extension instance.
|
|
|
|
* @param \Cms\Classes\Controller $controller The CMS controller object.
|
|
|
|
*/
|
2014-10-05 22:21:03 -07:00
|
|
|
public function __construct(Controller $controller = null)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
$this->controller = $controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of functions to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of functions
|
|
|
|
*/
|
|
|
|
public function getFunctions()
|
|
|
|
{
|
2014-06-28 21:23:13 +10:00
|
|
|
return [
|
2019-03-27 13:15:17 -06:00
|
|
|
new TwigSimpleFunction('page', [$this, 'pageFunction'], ['is_safe' => ['html']]),
|
|
|
|
new TwigSimpleFunction('partial', [$this, 'partialFunction'], ['is_safe' => ['html']]),
|
|
|
|
new TwigSimpleFunction('content', [$this, 'contentFunction'], ['is_safe' => ['html']]),
|
|
|
|
new TwigSimpleFunction('component', [$this, 'componentFunction'], ['is_safe' => ['html']]),
|
|
|
|
new TwigSimpleFunction('placeholder', [$this, 'placeholderFunction'], ['is_safe' => ['html']]),
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of filters this extensions provides.
|
|
|
|
*
|
|
|
|
* @return array An array of filters
|
|
|
|
*/
|
|
|
|
public function getFilters()
|
|
|
|
{
|
2014-06-28 21:23:13 +10:00
|
|
|
return [
|
2019-03-27 13:15:17 -06:00
|
|
|
new TwigSimpleFilter('page', [$this, 'pageFilter'], ['is_safe' => ['html']]),
|
|
|
|
new TwigSimpleFilter('theme', [$this, 'themeFilter'], ['is_safe' => ['html']]),
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of token parsers this extensions provides.
|
|
|
|
*
|
|
|
|
* @return array An array of token parsers
|
|
|
|
*/
|
|
|
|
public function getTokenParsers()
|
|
|
|
{
|
2014-06-28 21:23:13 +10:00
|
|
|
return [
|
2014-05-14 23:24:20 +10:00
|
|
|
new PageTokenParser,
|
|
|
|
new PartialTokenParser,
|
|
|
|
new ContentTokenParser,
|
|
|
|
new PutTokenParser,
|
|
|
|
new PlaceholderTokenParser,
|
|
|
|
new DefaultTokenParser,
|
|
|
|
new FrameworkTokenParser,
|
|
|
|
new ComponentTokenParser,
|
|
|
|
new FlashTokenParser,
|
|
|
|
new ScriptsTokenParser,
|
|
|
|
new StylesTokenParser,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a page.
|
|
|
|
* This function should be used in the layout code to output the requested page.
|
|
|
|
* @return string Returns the page contents.
|
|
|
|
*/
|
|
|
|
public function pageFunction()
|
|
|
|
{
|
|
|
|
return $this->controller->renderPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a partial.
|
|
|
|
* @param string $name Specifies the partial name.
|
|
|
|
* @param array $parameters A optional list of parameters to pass to the partial.
|
2017-11-19 14:58:47 +11:00
|
|
|
* @param bool $throwException Throw an exception if the partial is not found.
|
2014-05-14 23:24:20 +10:00
|
|
|
* @return string Returns the partial contents.
|
|
|
|
*/
|
2017-11-19 14:58:47 +11:00
|
|
|
public function partialFunction($name, $parameters = [], $throwException = false)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2017-11-19 14:58:47 +11:00
|
|
|
return $this->controller->renderPartial($name, $parameters, $throwException);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a content file.
|
2015-04-25 13:58:05 +10:00
|
|
|
* @param string $name Specifies the content block name.
|
|
|
|
* @param array $parameters A optional list of parameters to pass to the content.
|
2014-05-14 23:24:20 +10:00
|
|
|
* @return string Returns the file contents.
|
|
|
|
*/
|
2015-04-25 13:58:05 +10:00
|
|
|
public function contentFunction($name, $parameters = [])
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2015-04-25 13:58:05 +10:00
|
|
|
return $this->controller->renderContent($name, $parameters);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a component's default content.
|
2014-05-22 20:27:44 +10:00
|
|
|
* @param string $name Specifies the component name.
|
|
|
|
* @param array $parameters A optional list of parameters to pass to the component.
|
2014-05-14 23:24:20 +10:00
|
|
|
* @return string Returns the component default contents.
|
|
|
|
*/
|
2014-05-22 20:27:44 +10:00
|
|
|
public function componentFunction($name, $parameters = [])
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-05-22 20:27:44 +10:00
|
|
|
return $this->controller->renderComponent($name, $parameters);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders registered assets of a given type
|
|
|
|
* @return string Returns the component default contents.
|
|
|
|
*/
|
|
|
|
public function assetsFunction($type = null)
|
|
|
|
{
|
|
|
|
return $this->controller->makeAssets($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a placeholder content, without removing the block,
|
|
|
|
* must be called before the placeholder tag itself
|
|
|
|
* @return string Returns the placeholder contents.
|
|
|
|
*/
|
|
|
|
public function placeholderFunction($name, $default = null)
|
|
|
|
{
|
2014-10-11 01:42:04 +02:00
|
|
|
if (($result = Block::get($name)) === null) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return null;
|
2014-10-11 01:42:04 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2021-03-10 15:02:53 -06:00
|
|
|
$result = str_replace('<!-- X_WINTER_DEFAULT_BLOCK_CONTENT -->', trim($default), $result);
|
2014-05-14 23:24:20 +10:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-06-17 18:51:05 +10:00
|
|
|
/**
|
|
|
|
* Looks up the URL for a supplied page and returns it relative to the website root.
|
|
|
|
* @param mixed $name Specifies the Cms Page file name.
|
|
|
|
* @param array $parameters Route parameters to consider in the URL.
|
|
|
|
* @param bool $routePersistence By default the existing routing parameters will be included
|
|
|
|
* when creating the URL, set to false to disable this feature.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function pageFilter($name, $parameters = [], $routePersistence = true)
|
|
|
|
{
|
|
|
|
return $this->controller->pageUrl($name, $parameters, $routePersistence);
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* Converts supplied URL to a theme URL relative to the website root. If the URL provided is an
|
|
|
|
* array then the files will be combined.
|
|
|
|
* @param mixed $url Specifies the theme-relative URL
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function themeFilter($url)
|
|
|
|
{
|
|
|
|
return $this->controller->themeUrl($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a layout block.
|
|
|
|
* @param string $name Specifies the block name
|
|
|
|
*/
|
|
|
|
public function startBlock($name)
|
|
|
|
{
|
|
|
|
Block::startBlock($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a layout block contents and removes the block.
|
|
|
|
* @param string $name Specifies the block name
|
|
|
|
* @param string $default The default placeholder contents.
|
|
|
|
* @return mixed Returns the block contents string or null of the block doesn't exist
|
|
|
|
*/
|
|
|
|
public function displayBlock($name, $default = null)
|
|
|
|
{
|
2014-10-11 01:42:04 +02:00
|
|
|
if (($result = Block::placeholder($name)) === null) {
|
2014-10-16 22:36:00 -07:00
|
|
|
return $default;
|
2014-10-11 01:42:04 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2019-11-25 00:59:00 -05:00
|
|
|
/**
|
|
|
|
* @event cms.block.render
|
|
|
|
* Provides an opportunity to modify the rendered block content
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* Event::listen('cms.block.render', function ((string) $name, (string) $result) {
|
|
|
|
* if ($name === 'myBlockName') {
|
|
|
|
* return 'my custom content';
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
*/
|
2019-07-18 22:50:37 +08:00
|
|
|
if ($event = Event::fire('cms.block.render', [$name, $result], true)) {
|
2015-01-21 21:38:42 -08:00
|
|
|
$result = $event;
|
2019-07-18 22:50:37 +08:00
|
|
|
}
|
2015-01-21 21:38:42 -08:00
|
|
|
|
2021-03-10 15:02:53 -06:00
|
|
|
$result = str_replace('<!-- X_WINTER_DEFAULT_BLOCK_CONTENT -->', trim($default), $result);
|
2014-05-14 23:24:20 +10:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes a layout block.
|
|
|
|
*/
|
2014-09-06 17:10:52 +10:00
|
|
|
public function endBlock($append = true)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-09-06 17:10:52 +10:00
|
|
|
Block::endBlock($append);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2014-10-11 01:42:04 +02:00
|
|
|
}
|