mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-27 21:58:52 +00:00
Merge pull request #4131 from Nicofuma/ticket/13717
[ticket/13717] Set the assets after rendering the whole template
This commit is contained in:
commit
8e8051f41d
5 changed files with 175 additions and 48 deletions
95
phpBB/phpbb/template/assets_bag.php
Normal file
95
phpBB/phpbb/template/assets_bag.php
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This file is part of the phpBB Forum Software package.
|
||||||
|
*
|
||||||
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
||||||
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||||
|
*
|
||||||
|
* For full copyright and license information, please see
|
||||||
|
* the docs/CREDITS.txt file.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace phpbb\template;
|
||||||
|
|
||||||
|
class assets_bag
|
||||||
|
{
|
||||||
|
/** @var asset[] */
|
||||||
|
protected $stylesheets = [];
|
||||||
|
|
||||||
|
/** @var asset[] */
|
||||||
|
protected $scripts = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a css asset to the bag
|
||||||
|
*
|
||||||
|
* @param asset $asset
|
||||||
|
*/
|
||||||
|
public function add_stylesheet(asset $asset)
|
||||||
|
{
|
||||||
|
$this->stylesheets[] = $asset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a js script asset to the bag
|
||||||
|
*
|
||||||
|
* @param asset $asset
|
||||||
|
*/
|
||||||
|
public function add_script(asset $asset)
|
||||||
|
{
|
||||||
|
$this->scripts[] = $asset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all css assets
|
||||||
|
*
|
||||||
|
* @return asset[]
|
||||||
|
*/
|
||||||
|
public function get_stylesheets()
|
||||||
|
{
|
||||||
|
return $this->stylesheets;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all js assets
|
||||||
|
*
|
||||||
|
* @return asset[]
|
||||||
|
*/
|
||||||
|
public function get_scripts()
|
||||||
|
{
|
||||||
|
return $this->scripts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the HTML code to includes all css assets
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_stylesheets_content()
|
||||||
|
{
|
||||||
|
$output = '';
|
||||||
|
foreach ($this->stylesheets as $stylesheet)
|
||||||
|
{
|
||||||
|
$output .= "<link href=\"{$stylesheet->get_url()}\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\" />\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the HTML code to includes all js assets
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_scripts_content()
|
||||||
|
{
|
||||||
|
$output = '';
|
||||||
|
foreach ($this->scripts as $script)
|
||||||
|
{
|
||||||
|
$output .= "<script type=\"text/javascript\" src=\"{$script->get_url()}\"></script>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,8 @@
|
||||||
|
|
||||||
namespace phpbb\template\twig;
|
namespace phpbb\template\twig;
|
||||||
|
|
||||||
|
use phpbb\template\assets_bag;
|
||||||
|
|
||||||
class environment extends \Twig_Environment
|
class environment extends \Twig_Environment
|
||||||
{
|
{
|
||||||
/** @var \phpbb\config\config */
|
/** @var \phpbb\config\config */
|
||||||
|
@ -39,6 +41,9 @@ class environment extends \Twig_Environment
|
||||||
/** @var array **/
|
/** @var array **/
|
||||||
protected $namespace_look_up_order = array('__main__');
|
protected $namespace_look_up_order = array('__main__');
|
||||||
|
|
||||||
|
/** @var assets_bag */
|
||||||
|
protected $assets_bag;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
|
@ -63,6 +68,8 @@ class environment extends \Twig_Environment
|
||||||
$this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path();
|
$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->web_root_path = $this->phpbb_path_helper->get_web_root_path();
|
||||||
|
|
||||||
|
$this->assets_bag = new assets_bag();
|
||||||
|
|
||||||
$options = array_merge(array(
|
$options = array_merge(array(
|
||||||
'cache' => (defined('IN_INSTALL')) ? false : $cache_path,
|
'cache' => (defined('IN_INSTALL')) ? false : $cache_path,
|
||||||
'debug' => false,
|
'debug' => false,
|
||||||
|
@ -150,6 +157,16 @@ class environment extends \Twig_Environment
|
||||||
return $this->phpbb_path_helper;
|
return $this->phpbb_path_helper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the assets bag
|
||||||
|
*
|
||||||
|
* @return assets_bag
|
||||||
|
*/
|
||||||
|
public function get_assets_bag()
|
||||||
|
{
|
||||||
|
return $this->assets_bag;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the namespace look up order
|
* Get the namespace look up order
|
||||||
*
|
*
|
||||||
|
@ -173,6 +190,58 @@ class environment extends \Twig_Environment
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function render($name, array $context = [])
|
||||||
|
{
|
||||||
|
$output = parent::render($name, $context);
|
||||||
|
|
||||||
|
return $this->inject_assets($output);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function display($name, array $context = [])
|
||||||
|
{
|
||||||
|
$level = ob_get_level();
|
||||||
|
ob_start();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
parent::display($name, $context);
|
||||||
|
}
|
||||||
|
catch (\Exception $e)
|
||||||
|
{
|
||||||
|
while (ob_get_level() > $level)
|
||||||
|
{
|
||||||
|
ob_end_clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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.
|
* Loads a template by name.
|
||||||
*
|
*
|
||||||
|
|
|
@ -49,33 +49,20 @@ abstract class includeasset extends \Twig_Node
|
||||||
->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")
|
->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")
|
||||||
->write("\$asset->set_path(\$local_file, true);\n")
|
->write("\$asset->set_path(\$local_file, true);\n")
|
||||||
->outdent()
|
->outdent()
|
||||||
->write("\$asset->add_assets_version('{$config['assets_version']}');\n")
|
|
||||||
->write("\$asset_file = \$asset->get_url();\n")
|
|
||||||
->write("}\n")
|
->write("}\n")
|
||||||
|
->write("\$asset->add_assets_version('{$config['assets_version']}');\n")
|
||||||
->outdent()
|
->outdent()
|
||||||
->write("}\n")
|
->write("}\n")
|
||||||
->write("\$context['definition']->append('{$this->get_definition_name()}', '")
|
->write("\$context['definition']->set('STYLESHEETS', '__STYLESHEETS_PLACEHOLDER__');\n")
|
||||||
;
|
->write("\$context['definition']->set('SCRIPTS', '__SCRIPTS_PLACEHOLDER__');\n")
|
||||||
|
->write("\$this->getEnvironment()->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")
|
||||||
$this->append_asset($compiler);
|
|
||||||
|
|
||||||
$compiler
|
|
||||||
->raw("\n');\n")
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the definition name
|
* Get the name of the assets bag setter
|
||||||
*
|
*
|
||||||
* @return string (e.g. 'SCRIPTS')
|
* @return string (e.g. 'script')
|
||||||
*/
|
*/
|
||||||
abstract public function get_definition_name();
|
abstract public function get_setters_name();
|
||||||
|
|
||||||
/**
|
|
||||||
* Append the output code for the asset
|
|
||||||
*
|
|
||||||
* @param \Twig_Compiler A Twig_Compiler instance
|
|
||||||
* @return null
|
|
||||||
*/
|
|
||||||
abstract protected function append_asset(\Twig_Compiler $compiler);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,20 +18,8 @@ class includecss extends \phpbb\template\twig\node\includeasset
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function get_definition_name()
|
public function get_setters_name()
|
||||||
{
|
{
|
||||||
return 'STYLESHEETS';
|
return 'stylesheet';
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function append_asset(\Twig_Compiler $compiler)
|
|
||||||
{
|
|
||||||
$compiler
|
|
||||||
->raw("<link href=\"' . ")
|
|
||||||
->raw("\$asset_file . '\"")
|
|
||||||
->raw(' rel="stylesheet" type="text/css" media="screen" />')
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,20 +18,8 @@ class includejs extends \phpbb\template\twig\node\includeasset
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function get_definition_name()
|
public function get_setters_name()
|
||||||
{
|
{
|
||||||
return 'SCRIPTS';
|
return 'script';
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
protected function append_asset(\Twig_Compiler $compiler)
|
|
||||||
{
|
|
||||||
$compiler
|
|
||||||
->raw("<script type=\"text/javascript\" src=\"' . ")
|
|
||||||
->raw("\$asset_file")
|
|
||||||
->raw(". '\"></script>\n")
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue