-
-
Notifications
You must be signed in to change notification settings - Fork 55
Views
Keramat Jokar edited this page May 13, 2024
·
1 revision
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.
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);
}
}