Skip to content

Commit 48e40a7

Browse files
authored
Merge pull request #8 from Smile-SA/feat_introduce_custom_block
Ability to prevent loading the script
2 parents 5a0281c + 353c257 commit 48e40a7

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

Block/Script.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Smile\React\Block;
4+
5+
use Magento\Framework\View\Element\Template;
6+
7+
/**
8+
* React bundle loading block.
9+
* Argument disabled allows to disable the block output, hence the loading of the bundle
10+
* through (layout) argument injection.
11+
*
12+
* @category Smile
13+
* @package Smile\React
14+
*/
15+
class Script extends Template
16+
{
17+
/** @var bool */
18+
protected $disabled = false;
19+
20+
/**
21+
* Constructor.
22+
*
23+
* @param Template\Context $context Template context.
24+
*/
25+
public function __construct(
26+
Template\Context $context,
27+
array $data = []
28+
) {
29+
parent::__construct($context, $data);
30+
$this->disabled = (bool) ($data['disabled'] ?? false);
31+
}
32+
33+
/**
34+
* Render block HTML
35+
*
36+
* @return string
37+
*/
38+
protected function _toHtml()
39+
{
40+
if ($this->disabled) {
41+
return '';
42+
}
43+
44+
return parent::_toHtml();
45+
}
46+
}

view/frontend/layout/default.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
33
<body>
44
<referenceContainer name="before.body.end">
5-
<block name="react.script" template="Smile_React::script.phtml" />
5+
<block name="react.script" class="Smile\React\Block\Script" template="Smile_React::script.phtml" />
66
</referenceContainer>
77
</body>
88
</page>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Magento\Framework\View\Element\Template;
4+
use Smile\React\Block\Script;
55

6-
/** @var $block Template */
6+
/** @var $block Script */
77
?>
88
<script src="<?= '/js/react.bundle.js' ?>"></script>

0 commit comments

Comments
 (0)