1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-10 02:36:38 +02:00

Merge pull request #4165 from Nicofuma/ticket/14457

[ticket/14457] Uses a random placeholder to inject css and js
This commit is contained in:
Marc Alexander
2016-02-18 21:52:48 +01:00
9 changed files with 84 additions and 46 deletions

View File

@@ -19,10 +19,7 @@ namespace phpbb\template\twig;
class definition
{
/** @var array **/
protected $definitions = array(
'SCRIPTS' => '__SCRIPTS_PLACEHOLDER__',
'STYLESHEETS' => '__STYLESHEETS_PLACEHOLDER__'
);
protected $definitions = array();
/**
* Get a DEFINE'd variable

View File

@@ -195,9 +195,7 @@ class environment extends \Twig_Environment
*/
public function render($name, array $context = [])
{
$output = parent::render($name, $context);
return $this->inject_assets($output);
return $this->display_with_assets($name, $context);
}
/**
@@ -205,26 +203,25 @@ class environment extends \Twig_Environment
*/
public function display($name, array $context = [])
{
$level = ob_get_level();
ob_start();
echo $this->display_with_assets($name, $context);
}
try
{
parent::display($name, $context);
}
catch (\Exception $e)
{
while (ob_get_level() > $level)
{
ob_end_clean();
}
/**
* {@inheritdoc}
*/
private function display_with_assets($name, array $context = [])
{
$placeholder_salt = unique_id();
throw $e;
if (array_key_exists('definition', $context))
{
$context['definition']->set('SCRIPTS', '__SCRIPTS_' . $placeholder_salt . '__');
$context['definition']->set('STYLESHEETS', '__STYLESHEETS_' . $placeholder_salt . '__');
}
$output = ob_get_clean();
$output = parent::render($name, $context);
echo $this->inject_assets($output);
return $this->inject_assets($output, $placeholder_salt);
}
/**
@@ -234,10 +231,10 @@ class environment extends \Twig_Environment
*
* @return string
*/
private function inject_assets($output)
private function inject_assets($output, $placeholder_salt)
{
$output = str_replace('__STYLESHEETS_PLACEHOLDER__', $this->assets_bag->get_stylesheets_content(), $output);
$output = str_replace('__SCRIPTS_PLACEHOLDER__', $this->assets_bag->get_scripts_content(), $output);
$output = str_replace('__STYLESHEETS_' . $placeholder_salt . '__', $this->assets_bag->get_stylesheets_content(), $output);
$output = str_replace('__SCRIPTS_' . $placeholder_salt . '__', $this->assets_bag->get_scripts_content(), $output);
return $output;
}

View File

@@ -335,7 +335,7 @@ class twig extends \phpbb\template\base
return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
}
$this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle, $this->get_template_vars())));
$this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars()));
return $this;
}