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 databases, we can choose the following ways.

Using the object of Record class

This class is designed for working with extracted SQL data. 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
$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 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 delimeter "-*//*-", also in the end of path to 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 by field key of "enum" type
  • displayEnumOptions($field, [$selected]) - creates HTML from "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("Issue date", "date", "date"),
        array("Content", "text", "content")
    );
}

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 are 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 letters from a 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 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.