Creating a new template

Let’s assume we have a list of menu pages from "Pages" model: "About", "Gallery", "Contacts ". The main page is "About" (the value of link field is "index") and it's shown by "view-index.php" template view. Thus, Gallery and Contacts pages will be shown on "view-default.php" template.

Now we decide to display a paginated list of images from another model on "Gallery" page. To do this we need to delete gallery from “default” template and create an individual template for this page, where we will display our gallery.

  1. Assign the link field for "Gallery" page as “gallery”, now our page will be accessible by requesting "/gallery/".
  2. In "config/routes.php" add a new line and redirect “gallery/” request to new template.
    $mvFrontendRoutes = array(
    
    "index" => "view-index.php",
    
    "404" => "view-404.php",
    
    "default" => "view-default.php",
    
    "gallery/" => "view-gallery.php" 
    );
    
  3. In folder "views" create "view-gallery.php" file with the following content (we need the first 2 lines if we want to highlight the menu page, given by Pages model and show 404 error if the page not found or turned off.
    <?
    $content = $mv -> pages -> defineCurrentPage($mv -> router);
    $mv -> display404($content);
    include $mv -> views_path."main-header.php";
    ?>
    
    <?
    include $mv -> views_path."main-footer.php";
    ?>
    
  4. Now by "/gallery/" request include "view-gallery.php" template file, where we can add the gallery itself from another model. Select the simple gallery variant on MV that represents one field of simple model.
    <?
    class Gallery extends Model_Simple
    {
        protected $name = "Gallery";
    
        protected $model_elements = array(
            array("Gallery images", "multi_images", "photos")
        );
    }
    ?>
    
    CREATE TABLE "gallery" (
        "key" VARCHAR PRIMARY KEY NOT NULL, 
        "value" VARCHAR);
    
  5. Create "models/gallery.model.php" file, "gallery" table, add a new model into "config/models.php" and get admin section of "Gallery" in admin panel.
  6. The gallery object is accessible as "$mv -> gallery" and is a sample of simple model with only one field "photos". We take out photos, create thumb copies and wrap them into html tags.
    <div id="gallery">
        <?
            //Get the object of "Record" class
            $gallery_contents = $mv -> gallery -> loadIntoRecord();
    
            //Divide images into array, cut off the comments 
            $photos = $gallery_contents -> extractImages("photos", "no-comments");
    
            //Display images as thumbs and link to full size images
            foreach($photos as $photo) 
            {
                echo '<a class="lightbox" href="'.$mv -> root_path.$photo.'">';
                echo $mv -> gallery -> cropImage($photo, 200, 200)."</a>";
            }
        ?>
    </div>
    
  7. Final content of "view-gallery.php"
    <?
    $content = $mv -> pages -> defineCurrentPage($mv -> router);
    $mv -> display404($content);
    include $mv -> views_path."main-header.php";
    ?>
    
    <div id="gallery">
        <?
            $gallery_contents = $mv -> gallery -> loadIntoRecord();
            $photos = $gallery_contents -> extractImages("photos", "no-comments");
    
            foreach($photos as $photo)
            {
                echo '<a class="lightbox" href="'.$mv -> root_path.$photo.'">';
                echo $mv -> gallery -> cropImage($photo, 200, 200)."</a>";
            }
        ?>
    </div>
    
    <?
    include $mv -> views_path."main-footer.php";
    ?>