Skip to content
Keramat Jokar edited this page May 13, 2024 · 1 revision

What is a view?

Views are fragments of the final displayed websites, where you put all your layout code (HTML). To create a view, create a file in your module's views directory and save it as .tpl.

We make use of Smarty templating engine which replaces regular PHP in the views by a more HTML-like implementation.

Smarty 5 documentation

To load a view, use our templating class in your controller:

<?php

use MX\MX_Controller;

/**
 * MyPage Controller Class
 * @property mypage_model $mypage_model mypage_model Class
 */
class MyPage extends MX_Controller
{
    public function index()
    {
        // Load a view
        $output = $this->template->loadPage("myView.tpl");

        // Display the whole site with the content above inside it
        $this->template->view($output);
    }
}

To pass data to the view, pass it as an array in your loadPage call:

<?php

use MX\MX_Controller;

/**
 * MyPage Controller Class
 * @property mypage_model $mypage_model mypage_model Class
 */
class MyPage extends MX_Controller
{
    public function index()
    {
        // Example multi-dimensional array data
        $rows = [
            0 => [
                "id" => 1,
                "title" => "Test 1"
            ],

            1 => [
                "id" => 2,
                "title" => "Test 2"
            ],

            2 => [
                "id" => 3,
                "title" => "Test 3"
            ]
        ];

        // Define our data
        $data = [
            "date" => date("d/m"),
            "rows" => $rows
        ];

        // Load a view
        $output = $this->template->loadPage("myView.tpl", $data);

        // Display the whole site with the content above inside it
        $this->template->view($output);
    }
}
Clone this wiki locally