Output of data in template
Templates are created to display data from models. The models themselves are available as objects in the main $mv object and can also be instantiated separately. In order to display model data from the database, you can go the following ways.
Using the Record object
This class is designed to work with data extracted from the database. The object is usually created as a result of calling methods to find the desired record, which is then returned in the Record object.
<?
$record = $mv -> pages -> find(['url' => 'about', 'active' => 1]);
?>
<div class="main">
<h1><? echo $record -> name; ?></h1>
<div><? echo $record -> title; ?></div>
<div><? echo $record -> getEnumTitle('parent'); ?></div>
<div class="text"><? echo $record -> content; ?></div>
</div>
Model object methods
- resizeImage($image, $width, $height) - proportional reduction of the image, returns the img tag
- cropImage($image, $width, $height) - reduction and cropping of the image, returns the img tag (more details in the section Files and images)
- extractImages($value) - extracting images from a multi_images field that are stored in the database in JSON format
- getFirstImage($value) - extracting the first image from the image array for the multi_images type
- getEnumValues($field) - returns an array (key - value) of values of the enum type field
- getEnumTitle($field, $key) - returns the value (text) by the key of the enum type field
Let's assume there is a News model and we need to create a method to display a list of news.
class News extends Model
{
protected $name = 'News';
protected $model_elements = [
['Active', 'bool', 'active', ['on_create' => true]],
['Name', 'char', 'name', ['required' => true]],
['Date', 'date', 'date'],
['Content', 'text', 'content']
];
}
It is necessary to output a list of news with a title and a link to the full text of the news. Add a new method to the News model to output html.
<?
public function display()
{
//Extracting active records from the database
$rows = $this -> select(['active' => 1, 'order->desc' => 'date']);
//Variable for building html code
$html = '';
foreach($rows as $row)
{
//Link to news
$url = $this -> root_path.'news/'.$row['id'];
//Date and the news title wrapped in a link
$html .= '<div>';
$html .= '<div class="date">'.I18n::formatDate($row['date']).'</div>';
$html .= '<a href="'.$url.'">'.$row['name'].'</a>';
$html .= '</div>';
}
return $html;
}
Outputting data from simple models
If the model is of the Simple models type, then the data is extracted differently, since the simple model table contains only 2 columns: key and value. Additional information is available in the Managing simple models section.
class Options extends ModelSimple
{
protected $name = 'Options';
protected $model_elements = [
['Email for emails from the site', 'email', 'email', ['required' => true]],
['Phone', 'char', 'phone'],
['Slogan', 'text', 'slogan']
];
}
In the template, you can extract values by keys from the model object.
//Outputting data to the html template
echo $mv -> options -> phone;
echo $mv -> options -> slogan;
Previous section
Create a new template