Magento – M2.1 include block template in .phtml file

blocksmagento-2.1template

I followed this solution to add the user login and user create form in my template file but this not work. Can you notice when I did an error ? (I have my block showed bu not the content of the 2 login and register form).

First, I create this block :

<?php
namespace Test\Account\Block\Form;

class MyAccount extends \Magento\Framework\View\Element\Template
{
    /**
    * @var \Magento\Customer\Model\Session
    */
    protected $_sessionManager;

    /**
    * @param \Magento\Backend\Block\Template\Context $context
    * @param \Magento\Customer\Model\Session $sessionManager
    */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Customer\Model\Session $sessionManager,
        array $data = []
    ) {
        $this->_sessionManager = $sessionManager;
        parent::__construct($context, $data);
    }

    public function customerIsLoggedIn() {
        return $this->_sessionManager->isLoggedIn();
    }
}

Next, I checked the location of login and register html and block :
The html and block location of login/register block

I create this .phtml

<!-- Modal Trigger -->
<a class="btn" href="#loginRegister"><?php echo __('MY ACCOUNT'); ?></a>
<!-- Modal Structure -->
<div id="loginRegister" class="modal">
    <div class="modal-header right-align">
        <a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">x</a>
    </div>
    <div class="modal-content">
        <?php if ($this->customerIsLoggedIn()): ?>
            <p>CUSTOMER IS LOGIN</p>
        <?php else: ?>
            <p>CUSTOMER IS NOT LOGIN</p>
            <?php
            echo $this->getLayout()
            ->createBlock('Magento\Customer\Block\Form\Login\Info')
            ->setTemplate('Magento_Customer::newcustomer.phtm')
            ->toHtml();
            echo $this->getLayout()
            ->createBlock('Magento\Customer\Block\Form\Login')
            ->setTemplate('Magento_Customer::form/login.phtm')
            ->toHtml();
            ?>
        <?php endif; ?>
    </div>
</div>
<script type="text/javascript">
requirejs(['jquery', 'jquery.materialize'], function ($, jQueryMaterialize) {
    $('#loginRegister').modal();
});
</script>

And add my template by Test/Account/view/frontend/layout/default.xml

<?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework:View/Layout/etc/page_configuration.xsd'>
    <body>
        <!-- HEADER -->
        <referenceContainer name="shortcut.links">
            <block class="Test\Account\Block\Form\MyAccount" name="header.account-link" template="Test_Account::form/my_account.phtml"/>
        </referenceContainer>
    </body>
</page>

Best Answer

try like this

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('header-support')->toHtml();?>

header-support is identifier

in your case

  <?php echo $block->getLayout()->createBlock('Test\Account\Block\Form\MyAccount')->setBlockId('header.account-link')->toHtml();?>

like the way you can do it.