1

Can you please tell me where the assignment of the $block variable to the template occurs?

\Magento\Framework\View\TemplateEngine\Php::render()

I found :

 $tmpBlock = $this->_currentBlock;
 $this->_currentBlock = $block;
 extract($dictionary, EXTR_SKIP);
 include $fileName;
 $this->_currentBlock = $tmpBlock;

But I don’t understand how the $block variable gets into the current symbol table? From where is $block taken in the template?

3

As I can see, the $block was initialized when they call render() function. Example:

vendor/magento/framework/View/Element/Template.php:252

$html = $templateEngine->render($this->templateContext, $fileName, $this->_viewVars);

$this->templateContext is the $block that will be used on the template file

This is pure PHP feature, for more clearly, I created a test.php and template.php

  1. test.php
<?php

class Block
{
    public function test()
    {
        echo '1+++';
    }
}


class Test
{
    function render($block, $file, $directory)
    {
        include $file;
    }
}

$block = new Block();

$test = new Test();
$test->render($block, 'template.php', []);
  1. template.php
<?php

echo $block->test();
  1. My screen result:
1+++

You can see, I can use $block variable on template.php because I initialized it before including

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.