cancel
Showing results for 
Search instead for 
Did you mean: 

Copy block and insert into template

Copy block and insert into template

I have put the following code into my header.tpl file, but I believe it is failing on the getBlock('header') call.  $block returns false.  I had this working in Magento 1, and this is my first attempt to write the same thing in Magento 2.  Any guidance would be appreciated.

 

{{php}}

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

use \Magento\Framework\App\Bootstrap;
use \Magento\Store\Model\Store;
use \Magento\Store\Model\StoreManager;

include($_SERVER['DOCUMENT_ROOT']."app/bootstrap.php");
$_SERVER[StoreManager:Smiley TongueARAM_RUN_CODE] = 'default';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

/* @var Magento\Framework\App\State $state */
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

/* @var \Magento\Framework\View\LayoutInterface $layout */
$layout = $obj->create("\\Magento\\Framework\\View\\LayoutInterface");

echo 'one';

/* @var \Magento\Framework\View\Element\Messages $block */
$block = $layout->getBlock('header');

echo 'two';

//echo $block->toHtml();
//var_dump($block);

echo 'three';

{{/php}}