Skip to content

Commit ab83282

Browse files
committed
added child_html twig function for safe output of child blocks
1 parent df7c241 commit ab83282

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Framework/View/TemplateEngine/Twig.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Magento\Framework\ObjectManagerInterface;
66
use Magento\Framework\Event\ManagerInterface;
7+
use Magento\Framework\View\Element\AbstractBlock;
78
use Magento\Framework\View\TemplateEngine\Php;
89
use Magento\Framework\App\Helper\AbstractHelper;
910
use Magento\Framework\View\Element\BlockInterface;
@@ -87,6 +88,11 @@ private function initTwig()
8788
$this->twig->addFunction(new \Twig\TwigFunction('layoutBlock', [$this, 'layoutBlock']));
8889
$this->twig->addFunction(new \Twig\TwigFunction('get*', [$this, 'catchGet']));
8990
$this->twig->addFunction(new \Twig\TwigFunction('isset', [$this, '__isset']));
91+
$this->twig->addFunction(new \Twig\TwigFunction('child_html', [$this, 'getChildHtml'], [
92+
'needs_context' => true,
93+
'is_safe' => ['html']
94+
]));
95+
9096
$this->twig->addFilter(new \Twig\TwigFilter('trans', '__'));
9197
$this->twig->addExtension(new \Twig\Extension\DebugExtension());
9298
foreach ($this->getDefinedFunctionsInHelpersFile() as $functionName) {
@@ -198,6 +204,18 @@ public function helper($className)
198204
return $helper;
199205
}
200206

207+
public function getChildHtml(array $context, $alias = '', $useCache = true)
208+
{
209+
if (!isset($context['block'])) {
210+
return null;
211+
}
212+
$block = $context['block'];
213+
if (!$block instanceof AbstractBlock) {
214+
return null;
215+
}
216+
return $block->getChildHtml($alias, $useCache);
217+
}
218+
201219
/**
202220
* @return array
203221
* @throws \Magento\Framework\Exception\FileSystemException

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"twig/twig": "~2.0"
1010
},
1111
"type": "magento2-module",
12-
"version": "1.8.0",
12+
"version": "1.8.1",
1313
"license": [
1414
"OSL-3.0"
1515
],

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
2-
<module name="SchumacherFM_Twig" setup_version="1.1.4">
2+
<module name="SchumacherFM_Twig" setup_version="1.8.1">
33
<sequence>
44
<module name="Magento_Core" />
55
<module name="Magento_Backend" />

0 commit comments

Comments
 (0)