2

I am following the 'Fundamentals of Magento 2' course and, I have to say, I am getting really frustrated with the example code being wrong on every exercise!

I am trying to set a template for a custom block within a controller. My folder structure is:

- /app/code/Training/Rendering/
    -- Block/Template.php
    -- Controller/Block/Template.php
    -- view/frontend/template.phtml

The routes are working fine as I can print output on the page from the controller. The files mentioned above (in order) contain the following:

/app/code/Training/Rendering/Block/Template.php:

<?php

namespace Training\Rendering\Block;


/**
 * Custom template block
 *
 * @package Training\Rendering\Block
 */
class Template extends \Magento\Framework\View\Element\Template
{

}

app/code/Training/Rendering/Controller/Block/Template.php:

<?php

namespace Training\Rendering\Controller\Block;

use Magento\Framework\App\Action\Action;

/**
 * Template Action Class
 * URL: testblock/Block/Template
 *
 * @package Training\Rendering\Controller\Block
 */
class Template extends Action
{

    /**
     * Execute Dispatch
     */
    public function execute()
    {
        $block = $this->_view->getLayout()->createBlock('Training\Rendering\Block\Template');
        $block->setTemplate('template.phtml');
        $this->getResponse()->appendBody($block->toHtml());
    }
}

app/code/Training/Rendering/view/frontend/template.phtml:

<p>Hello from custom block template!</p>

I have tried changing the controller code to

..->createBlock('Magento\Framework\View\Element\Template'); to test but no joy!

The problem is that the template (phtml) is not being loaded.

The code is exactly the same as the exercise (I tried copying and pasting from the PDF!).

I do understand that this will be handled with XML in the future, but in the interest of getting passed this exercise, I just need it to work like this.

Thanks in advance for any help.

4
  • which error are display? Commented Feb 8, 2017 at 6:49
  • @Rakesh There are no errors. Blank screen. If I echo text from the execute function, it is displayed. I just can't get the contents of template.phtml to load into the block
    – Wildcard27
    Commented Feb 8, 2017 at 6:50
  • Are you want to get content of template file, have you check your execute method is called? Commented Feb 8, 2017 at 6:52
  • @Rakesh execute method is definitely called. As I just said, I can echo text from the method and the text is displayed on the screen under the correct URL
    – Wildcard27
    Commented Feb 8, 2017 at 6:54

2 Answers 2

4

First your view template file directory is wrong, You had missing templates folder in your path,

app/code/Training/Rendering/view/frontend/templates/template.phtml:

<p>Hello from custom block template!</p>

call execute function with below code, now call template like below,

$block->setTemplate('Training_Rendering::template.phtml');

 public function execute()
        {
            $block = $this->_view->getLayout()->createBlock('Training\Rendering\Block\Template');
            $block->setTemplate('Training_Rendering::template.phtml');
            $this->getResponse()->appendBody($block->toHtml());
        }
5
  • 1
    Wow. This exercise guide is really messed up! Thank you very much for your help!
    – Wildcard27
    Commented Feb 8, 2017 at 7:00
  • You can close this question after mark as accepted. Commented Feb 8, 2017 at 7:01
  • Just had to wait before I could mark as answered. Done now.
    – Wildcard27
    Commented Feb 8, 2017 at 7:02
  • Just one question, Rakesh: How did you know to set the template this way? I would assume it to be in the Magento docs but I can't find it
    – Wildcard27
    Commented Feb 8, 2017 at 22:41
  • @RakeshJesadiya, the javascript written in this file not working, how can we print the phtml file js code in browser console Commented Nov 6, 2020 at 8:59
-2

Create object of PageFactory in controller and return it.

example:-

return $this->pageFactory->create();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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