Getting started

Models

Views and routes

Forms

SQL queries

Sessions and security

Plugins

Documentation

Data display in template

Templates are created to display data from models. The models themselves are accessible as objects in main object "$mv". In order to display model data from a database, we can choose the following ways.

Using the object of Record class

This class is designed for working with extracted SQL strings. The object is usually created as a result of search query methods of required record, which returns into Record object. Read more about how to retrieve data from such an object in Object of Record class section.

//Regular model
$content = $mv -> pages -> findRecord(array("url" => "events", "active" => 1));
$last_event = $mv -> events -> findRecord(array("active" => 1, "limit->" => 1, "order->desc" => "date"));

//Simple model, content is loaded into Record object
$gallery_data = $mv -> gallery -> loadIntoRecord();

The methods of model object

  • resizeImage($image, $width, $height) – resize the image, returns "img" tag
  • cropImage($image, $width, $height) - image resize and crop, returns "img" tag (read more in Files and images section)
  • extractImages($value [, $no-comments]) - extract images from the field of "multi_images" type. Data of "multi_images" type are stored in database as one text fragment which is paths to images on local disc. The paths are concatenated by a delimiter "-*//*-", also in the end of path of the file can be a comment to this image between "(**)" symbols. When extracting data of this type from a database you should separate images and cut off the comments from them. If in a result array no comments are needed, you should pass the value of optional parameter as "no-comments".
  • getFirstImage($value) - get the first image from images array ("multi_images" type)
  • getEnumValues($field) - returns an array (key-value) of values for "enum" type field
  • getEnumTitle($field, $key) - returns a value (title) by field key of "enum" type
  • displayEnumOptions($field, [$selected]) - creates HTML code with "option" tags for "select" element, used to display fields of "enum" type, in the optional parameter "$selected" it is possible to pass a value that should be selected in the list by default.

Creating methods inside a model to generate HTML code

Suppose there is News model.

class News extends Model
{
    protected $name = "News";
 
    protected $model_elements = array(
        array("Activate", "bool", "active", array("on_create" => true)),
        array("Title", "char", "name", array("required" => true)),
        array("URL", "url", "url", array("unique" => true, "translit_from" => "name")),
        array("Date", "date", "date"),
        array("Content", "text", "content", array("rich_text" => true))
    );
}

We need to display the news list with titles and links to full content of news. In News model add a new method to display HTML.

public function display()
{
   //Extract required records from a database
   $rows = $this -> select(array("order->desc" => "date", "limit->" => $this -> pager -> getParamsForSQL()));
 
   $html = ""; //A variable to keep HTML code 
 
   foreach($rows as $row)
   {
      $url = $this -> root_path."news/".($row['url'] ? $row['url']."/" : $row['id']."/"); //Link to news 
 
      $html .= '<p class="date">'.I18n :: formatDate($row['date']).'</p>'; //Date and title of news 
      $html .= '<a class="title" href="'.$url.'">'.$row['name'].'</a>';
   }
 
   return $html;
}

Display data from simple models

If the model is related to simple models type, then data is extracted in a different way, since the table of Simple model contains only 2 columns: key and value. Read more in Simple models management.

class Options extends Model_Simple
{
   protected $name = "Additional parameters";
 
   protected $model_elements = array(
       array("Email for messages from website", "email", "email", array("required" => true)),
       array("Phone", "char", "phone"),
       array("Slogan", "text", "slogan")
   );
};

In the template it is possible to extract values by keys from model objects, or create an Object of Record class, if a more complex data processing is required when inserting data into a template.

//Insert of data into html template
<div id="header">
   <p><? echo $mv -> options -> phone; ?></p>
   <h3><? echo $mv -> options -> slogan; ?></h3>
</div>

//Extract e-mail address to send a message 
if($form -> isValid())
{  
    $recipient = $mv -> options -> email;
    $message = $form -> composeMessage(array("head", "company", "name", "email", "telephone"));

    Email :: send($recipient, "A message from a website", $message);
    $mv -> redirect("order/?send");
}

Example of all data extraction of simple model into Object of Record class. This methos is often used if there are Files and images data.

$params = $mv -> options -> loadIntoRecord();

echo $params -> phone;
echo $params -> content;

//Another way to get simple model data
echo $mv -> options -> phone;
echo $mv -> options -> content;

Attention! When using Record object with a simple model there is no way to update data as described in Records management section.