Magento 2 Passing Data from Controller to Template

In the today’s lesson we’ll discuss:

  • How to pass data from a controller class to a template?
  • How to get access to a block class from a controller?
  • How to assign data to a block class?
  • How to render dynamic data in a template?

From the Template and Layout Rendering lesson, we've learned how to create a new template and render it on a custom page with the help of a layout. As you've noticed, the template provides HTML markup with static text.

This might be enough for a static website, but not for Magento 2 website. In this lesson, we are going to learn how to pass data from the PHP side, specifically from an Action Controller to the template.

By the end of this lesson, you will know one of the ways how dynamic content can be rendered on a page.Watch in the tutorial.

https://youtu.be/Cm9uiFH_ttI