MV framework logo
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Documentation
Download .zip version 3.2.0 from 25.12.2024
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
Documentation
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker

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>
For more information about records, see Record object.

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;
}
To output long lists, you will additionally need pagination.

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

Next section

Record object
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github