N°2847 - Separate inline scripts and css from html in the rendering of pages

This commit is contained in:
Eric
2020-09-18 09:36:39 +02:00
parent e83dfe5982
commit 9cd719ab56
28 changed files with 310 additions and 378 deletions

View File

@@ -37,10 +37,29 @@ class BlockRenderer
public const TWIG_BASE_PATH = APPROOT.'templates/';
/** @var string[] TWIG_ADDITIONAL_PATHS Additional paths for resources to be loaded either as a template or as an image, ... */
public const TWIG_ADDITIONAL_PATHS = [APPROOT.'images/'];
/** @var \Twig_Environment $oTwigEnv Singleton used during rendering */
protected static $oTwigEnv;
/**
* BlockRenderer constructor.
*
* @param \Combodo\iTop\Application\UI\iUIBlock $oBlock
* @param array $aContextParams
*
* @throws \Twig\Error\LoaderError
*/
public function __construct(iUIBlock $oBlock, array $aContextParams = [])
{
if (null === static::$oTwigEnv) {
static::$oTwigEnv = TwigHelper::GetTwigEnvironment(static::TWIG_BASE_PATH, static::TWIG_ADDITIONAL_PATHS);
}
$this->oBlock = $oBlock;
$this->aContextParams = $aContextParams;
$this->ResetRenderingOutput();
}
/**
* Helper to use directly in TWIG to render a block and its sub blocks
*
@@ -57,7 +76,7 @@ class BlockRenderer
{
$oSelf = new static($oBlock, $aContextParams);
return $oSelf->RenderTemplates();
return $oSelf->RenderHtml();
}
/** @var \Combodo\iTop\Application\UI\iUIBlock $oBlock */
@@ -67,26 +86,6 @@ class BlockRenderer
/** @var \Combodo\iTop\Renderer\RenderingOutput $oRenderingOutput */
protected $oRenderingOutput;
/**
* BlockRenderer constructor.
*
* @param \Combodo\iTop\Application\UI\iUIBlock $oBlock
* @param array $aContextParams
*
* @throws \Twig\Error\LoaderError
*/
public function __construct(iUIBlock $oBlock, array $aContextParams = [])
{
if (null === static::$oTwigEnv)
{
static::$oTwigEnv = TwigHelper::GetTwigEnvironment(static::TWIG_BASE_PATH, static::TWIG_ADDITIONAL_PATHS);
}
$this->oBlock = $oBlock;
$this->aContextParams = $aContextParams;
$this->ResetRenderingOutput();
}
/**
* Reset the rendering output so it can be computed again
*
@@ -98,29 +97,6 @@ class BlockRenderer
return $this;
}
/**
* Return the processed rendering output.
*
* @return \Combodo\iTop\Renderer\RenderingOutput
* @throws \ReflectionException
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \Exception
*/
public function GetRenderingOutput()
{
$this->ResetRenderingOutput();
$this->oRenderingOutput->AddHtml($this->RenderHtml())
->AddCss($this->RenderCssInline())
->AddJs($this->RenderJsInline())
->SetCssFiles($this->GetCssFiles())
->SetJsFiles($this->GetJsFiles());
return $this->oRenderingOutput;
}
/**
* Return the raw output of the HTML template
*
@@ -196,46 +172,6 @@ class BlockRenderer
return $sOutput;
}
/**
* Return the cumulated HTML output of the CSS, HTML and JS templates
*
* @return string
* @throws \ReflectionException
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function RenderTemplates()
{
$sOutput = '';
// CSS first to avoid visual glitches
$sCssOutput = $this->RenderCssInline();
if(!empty($sCssOutput))
{
$sOutput .= <<<HTML
<style>
{$sCssOutput}
</style>
HTML;
}
$sOutput .= $this->RenderHtml();
// JS last so all markup is build and ready
$sJsOutput = $this->RenderJsInline();
if(!empty($sJsOutput))
{
$sOutput .= <<<HTML
<script type="text/javascript">
{$sJsOutput}
</script>
HTML;
}
return $sOutput;
}
/**
* Return an array of the absolute URL of the block JS files
*
@@ -266,7 +202,6 @@ HTML;
*/
protected function GetTemplateParameters()
{
return array_merge(['oUIBlock' => $this->oBlock], $this->aContextParams);
return array_merge(['oUIBlock' => $this->oBlock], $this->aContextParams, $this->oBlock->GetParameters());
}
}