Create a System Twig extension, for shared features

This commit is contained in:
Sam Georges 2014-06-26 17:32:12 +10:00
parent 7755714981
commit 9d2d9b621f
4 changed files with 84 additions and 14 deletions

View File

@ -14,7 +14,8 @@ use Exception;
use Twig_Environment;
use Controller as BaseController;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\Extension as TwigExtension;
use Cms\Twig\Extension as CmsTwigExtension;
use System\Twig\Extension as SystemTwigExtension;
use Cms\Classes\FileHelper as CmsFileHelper;
use System\Classes\ErrorHandler;
use October\Rain\Support\Markdown;
@ -229,7 +230,8 @@ class Controller extends BaseController
$options['cache'] = storage_path().'/twig';
$this->twig = new Twig_Environment($this->loader, $options);
$this->twig->addExtension(new TwigExtension($this));
$this->twig->addExtension(new CmsTwigExtension($this));
$this->twig->addExtension(new SystemTwigExtension);
}
/**

View File

@ -86,7 +86,6 @@ class Extension extends Twig_Extension
public function getFilters()
{
$filters = [
new Twig_SimpleFilter('app', [$this, 'appFilter'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('page', [$this, 'pageFilter'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('theme', [$this, 'themeFilter'], ['is_safe' => ['html']]),
];
@ -211,16 +210,6 @@ class Extension extends Twig_Extension
return $this->controller->themeUrl($url);
}
/**
* Converts supplied URL to one relative to the website root.
* @param mixed $url Specifies the application-relative URL
* @return string
*/
public function appFilter($url)
{
return URL::to($url);
}
/**
* 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.

View File

@ -13,6 +13,7 @@ use System\Classes\PluginManager;
use System\Classes\SettingsManager;
use System\Twig\Engine as TwigEngine;
use System\Twig\Loader as TwigLoader;
use System\Twig\Extension as TwigExtension;
use System\Models\EmailSettings;
use System\Models\EmailTemplate;
use Backend\Classes\WidgetManager;
@ -74,7 +75,9 @@ class ServiceProvider extends ModuleServiceProvider
* Register basic twig
*/
App::bindShared('twig', function($app) {
return new Twig_Environment(new TwigLoader(), ['auto_reload' => true]);
$twig = new Twig_Environment(new TwigLoader(), ['auto_reload' => true]);
$twig->addExtension(new TwigExtension);
return $twig;
});
/*

View File

@ -0,0 +1,76 @@
<?php namespace System\Twig;
use URL;
use Twig_Extension;
use Twig_TokenParser;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
use System\Classes\ApplicationException;
/**
* The System Twig extension class implements common Twig functions and filters.
*
* @package october\system
* @author Alexey Bobkov, Samuel Georges
*/
class Extension extends Twig_Extension
{
/**
* Creates the extension instance.
*/
public function __construct(){}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'System';
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
public function getFunctions()
{
return [];
}
/**
* Returns a list of filters this extensions provides.
*
* @return array An array of filters
*/
public function getFilters()
{
$filters = [
new Twig_SimpleFilter('app', [$this, 'appFilter'], ['is_safe' => ['html']]),
];
return $filters;
}
/**
* Returns a list of token parsers this extensions provides.
*
* @return array An array of token parsers
*/
public function getTokenParsers()
{
return [];
}
/**
* Converts supplied URL to one relative to the website root.
* @param mixed $url Specifies the application-relative URL
* @return string
*/
public function appFilter($url)
{
return URL::to($url);
}
}