diff options
| author | Tristan Darricau <tristan.darricau@sensiolabs.com> | 2016-01-17 20:11:36 +0100 |
|---|---|---|
| committer | Tristan Darricau <tristan.darricau@sensiolabs.com> | 2016-01-25 00:31:45 +0100 |
| commit | 52b3d54805af6d0a89ede1cd2acac92cd95693c0 (patch) | |
| tree | 83b86f33995d5cdc69d54e73b8da2d3379e41c98 /phpBB/phpbb/template/twig/environment.php | |
| parent | d8af5ac4f9ddc1de16af73f5af1f2c465ee124e4 (diff) | |
| download | forums-52b3d54805af6d0a89ede1cd2acac92cd95693c0.tar forums-52b3d54805af6d0a89ede1cd2acac92cd95693c0.tar.gz forums-52b3d54805af6d0a89ede1cd2acac92cd95693c0.tar.bz2 forums-52b3d54805af6d0a89ede1cd2acac92cd95693c0.tar.xz forums-52b3d54805af6d0a89ede1cd2acac92cd95693c0.zip | |
[ticket/13717] Set the assets after rendering the whole template
The goal being to be able to call INCLUDECSS/JS from anywhere in any tempalte or event
PHPBB3-13717
Diffstat (limited to 'phpBB/phpbb/template/twig/environment.php')
| -rw-r--r-- | phpBB/phpbb/template/twig/environment.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index 6e75403159..709505a75f 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -13,6 +13,8 @@ namespace phpbb\template\twig; +use phpbb\template\assets_bag; + class environment extends \Twig_Environment { /** @var \phpbb\config\config */ @@ -39,6 +41,9 @@ class environment extends \Twig_Environment /** @var array **/ protected $namespace_look_up_order = array('__main__'); + /** @var assets_bag */ + protected $assets_bag; + /** * Constructor * @@ -63,6 +68,8 @@ class environment extends \Twig_Environment $this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path(); $this->web_root_path = $this->phpbb_path_helper->get_web_root_path(); + $this->assets_bag = new assets_bag(); + $options = array_merge(array( 'cache' => (defined('IN_INSTALL')) ? false : $cache_path, 'debug' => false, @@ -151,6 +158,16 @@ class environment extends \Twig_Environment } /** + * Gets the assets bag + * + * @return assets_bag + */ + public function get_assets_bag() + { + return $this->assets_bag; + } + + /** * Get the namespace look up order * * @return array @@ -174,6 +191,43 @@ class environment extends \Twig_Environment } /** + * {@inheritdoc} + */ + public function render($name, array $context = []) + { + $output = parent::render($name, $context); + + return $this->inject_assets($output); + } + + /** + * {@inheritdoc} + */ + public function display($name, array $context = []) + { + ob_start(); + parent::display($name, $context); + $output = ob_get_clean(); + + echo $this->inject_assets($output); + } + + /** + * Injects the assets (from INCLUDECSS/JS) in the output. + * + * @param string $output + * + * @return string + */ + private function inject_assets($output) + { + $output = str_replace('__STYLESHEETS_PLACEHOLDER__', $this->assets_bag->get_stylesheets_content(), $output); + $output = str_replace('__SCRIPTS_PLACEHOLDER__', $this->assets_bag->get_scripts_content(), $output); + + return $output; + } + + /** * Loads a template by name. * * @param string $name The template name |
