N°2847 - Fix content parameters (eg. aPage) not passed in subblocks

This commit is contained in:
Molkobain
2020-08-06 17:07:08 +02:00
parent 6666f3d033
commit 1e3771dffa
5 changed files with 26 additions and 20 deletions

View File

@@ -41,6 +41,7 @@ class BlockRenderer
* Helper to use directly in TWIG to render a block and its sub blocks
*
* @param \Combodo\iTop\Application\UI\iUIBlock $oBlock
* @param array $aContextParams
*
* @return string
* @throws \ReflectionException
@@ -48,14 +49,16 @@ class BlockRenderer
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public static function RenderBlockTemplates(iUIBlock $oBlock)
public static function RenderBlockTemplates(iUIBlock $oBlock, $aContextParams = [])
{
$oSelf = new static($oBlock);
$oSelf = new static($oBlock, $aContextParams);
return $oSelf->RenderTemplates();
}
/** @var \Combodo\iTop\Application\UI\iUIBlock $oBlock */
protected $oBlock;
/** @var array $aContextParams Optional context parameters to pass to the template during rendering */
protected $aContextParams;
/** @var \Combodo\iTop\Renderer\RenderingOutput $oRenderingOutput */
protected $oRenderingOutput;
@@ -63,8 +66,9 @@ class BlockRenderer
* BlockRenderer constructor.
*
* @param \Combodo\iTop\Application\UI\iUIBlock $oBlock
* @param array $aContextParams
*/
public function __construct(iUIBlock $oBlock)
public function __construct(iUIBlock $oBlock, $aContextParams = [])
{
if(null === static::$oTwigEnv)
{
@@ -72,6 +76,7 @@ class BlockRenderer
}
$this->oBlock = $oBlock;
$this->aContextParams = $aContextParams;
$this->ResetRenderingOutput();
}
@@ -125,7 +130,7 @@ class BlockRenderer
{
$sOutput = TwigHelper::RenderTemplate(
static::$oTwigEnv,
[$this->GetBlockParameterNameForTemplate() => $this->oBlock],
$this->GetTemplateParameters(),
$this->oBlock::GetHtmlTemplateRelPath(),
TwigHelper::ENUM_FILE_TYPE_HTML
);
@@ -150,7 +155,7 @@ class BlockRenderer
{
$sOutput = TwigHelper::RenderTemplate(
static::$oTwigEnv,
[$this->GetBlockParameterNameForTemplate() => $this->oBlock],
$this->GetTemplateParameters(),
$this->oBlock::GetJsTemplateRelPath(),
TwigHelper::ENUM_FILE_TYPE_JS
);
@@ -175,7 +180,7 @@ class BlockRenderer
{
$sOutput = TwigHelper::RenderTemplate(
static::$oTwigEnv,
[$this->GetBlockParameterNameForTemplate() => $this->oBlock],
$this->GetTemplateParameters(),
$this->oBlock::GetCssTemplateRelPath(),
TwigHelper::ENUM_FILE_TYPE_CSS
);
@@ -247,13 +252,14 @@ HTML;
}
/**
* Return the name of the parameter used in the template to access the block object (class short name = without namespace)
* Return an associative array of parameters for the template.
* Contains oUIBlock for the current block and optionally some others.
*
* @return string
* @throws \ReflectionException
* @return array
*/
protected function GetBlockParameterNameForTemplate()
protected function GetTemplateParameters()
{
return 'oUIBlock';
return array_merge(['oUIBlock' => $this->oBlock], $this->aContextParams);
}
}

View File

@@ -150,8 +150,8 @@ class Extension
// Function to render a UI block (HTML, inline CSS, inline JS) and its sub blocks directly in the TWIG
// Usage in twig: {{ render_block(oBlock) }}
/** @since 2.8.0 */
$oTwigEnv->addFunction(new Twig_SimpleFunction('render_block', function(iUIBlock $oBlock){
return BlockRenderer::RenderBlockTemplates($oBlock);
$oTwigEnv->addFunction(new Twig_SimpleFunction('render_block', function(iUIBlock $oBlock, $aContextParams = []){
return BlockRenderer::RenderBlockTemplates($oBlock, $aContextParams);
}, ['is_safe' => ['html']]));
}

View File

@@ -3,7 +3,7 @@
{% if aSection.aItems|length > 0 %}
<div class="ibo-popover-menu--section" data-role="ibo-popover-menu--section">
{% for oUIBlockItem in aSection.aItems %}
{{ render_block(oUIBlockItem) }}
{{ render_block(oUIBlockItem, {aPage: aPage}) }}
{% endfor %}
</div>
{% endif %}

View File

@@ -1,13 +1,13 @@
<nav id="{{ oUIBlock.Id }}" class="ibo-top-bar">
<div class="ibo-top-bar--quick-actions">
{% if oUIBlock.HasQuickCreate %}
{{ render_block(oUIBlock.QuickCreate) }}
{{ render_block(oUIBlock.QuickCreate, {aPage: aPage}) }}
{% endif %}
{% if oUIBlock.HasGlobalSearch %}
{{ render_block(oUIBlock.GlobalSearch) }}
{{ render_block(oUIBlock.GlobalSearch, {aPage: aPage}) }}
{% endif %}
</div>
{% if oUIBlock.HasBreadcrumbs %}
{{ render_block(oUIBlock.Breadcrumbs) }}
{{ render_block(oUIBlock.Breadcrumbs, {aPage: aPage}) }}
{% endif %}
</nav>

View File

@@ -33,14 +33,14 @@
{% endblock %}
</head>
<body data-gui-type="backoffice">
{{ render_block(aLayouts.oNavigationMenu) }}
{{ render_block(aLayouts.oNavigationMenu, {aPage: aPage}) }}
<div id="ibo-page-container">
<div id="ibo-top-container">
{{ include('pages/backoffice/extension-blocks/banner.html.twig') }}
{{ render_block(aLayouts.oTopBar) }}
{{ render_block(aLayouts.oTopBar, {aPage: aPage}) }}
</div>
{{ include('pages/backoffice/extension-blocks/header.html.twig') }}
{{ render_block(aLayouts.oPageContent) }}
{{ render_block(aLayouts.oPageContent, {aPage: aPage}) }}
{{ include('pages/backoffice/extension-blocks/footer.html.twig') }}
{# TODO: Remove this when modal development is done #}