2

I have a page that I want to add content to it. My problem is that my block is initialized and setTemplate is called but my template never rendered. I tried adding module name to layout (Vendor_Test::) but it made no difference. When I edit my layout, I change the title and clean the cache, so I can see that new title is applied.

Here is my files:

Controller:

<?php
namespace Vendor\Test\Controller\Exec;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\ResponseInterface;

class Bank extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
   public function execute()
    {
        $page = $this->resultPageFactory->create();
        return $page;
    }
}

My layout:

<?xml version="1.0"?>
<page layout="3column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Bank Page Title</title>
    </head>
    <body>
        <block class="Vendor\Test\Block\Date" name="vendor.test.date" template="date.phtml"   >
            <arguments>
                <argument name="something" xsi:type="string">Value</argument>
            </arguments>
        </block>
    </body>
</page>

My Block:

<?php 
namespace Vendor\Test\Block;


class Date extends \Magento\Framework\View\Element\Template
{
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(\Magento\Framework\View\Element\Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function getDate()
    {
        return date('m/d/Y H:i:s');
    }
}

My template:

<div class="Something" >
    <?php  echo $block->getDate();?>
</div>

Output:

Output

1

Change your layout file by following code


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Bank Page Title</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Test\Block\Date" name="vendor.test.date" template="Vendor_Test::date.phtml">
                <arguments>
                    <argument name="something" xsi:type="string">Value</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Clear cache.

0

In my case, I had some function calls in my template file. As one of the functions which were throwing some error. That was causing the template file from not rendering. So, anything after that function call will not display.

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.