1

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.

  • which error are display? – Rakesh Jesadiya Feb 8 '17 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 Feb 8 '17 at 6:50
  • Are you want to get content of template file, have you check your execute method is called? – Rakesh Jesadiya Feb 8 '17 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 Feb 8 '17 at 6:54
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());
        }
  • 1
    Wow. This exercise guide is really messed up! Thank you very much for your help! – Wildcard27 Feb 8 '17 at 7:00
  • You can close this question after mark as accepted. – Rakesh Jesadiya Feb 8 '17 at 7:01
  • Just had to wait before I could mark as answered. Done now. – Wildcard27 Feb 8 '17 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 Feb 8 '17 at 22:41
-1

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, privacy policy and cookie policy

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