Add a View to Joomla Site Part

When no task is given in the request variables, the default task is display. The BaseController class has such a task. When the display is used, the 'view' variable decides what is displayed on the page.

1. View File

src/View/Planet/HtmlView.php

The View gets the data from the Model and then calls the parent display method.

use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;

class HtmlView extends BaseHtmlView
{
   protected $item;
    
    public function display($tpl = null)
    {
       $this->item = $this->get('Item');
        
        parent::display($tpl);
    }
}

This method will display data using the default.php layout file in the tmpl/planet folder.

Here, the model is used to get the data from the database. This is very similar to the backend functionality. This calls getItem() method in the Model class.

2. Layout File

site/tmpl/planet/default.php

<h1><?php echo $this->item->title; ?></h1>

This template file is included by the HtmlView class. Here, $this refers to the HtmlView class.