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

Create a new template

Let's assume there is a list of menu pages from the Pages model: 

  • About us
  • Gallery
  • Contacts

The main page is the 'About us' page and it is displayed using the view-index.php template. Thus, the 'Gallery' and 'Contacts' pages are displayed using the view-default.php template.

Now we decide to display a list of images from another model on the 'Gallery' page. To do this, we need to remove the gallery from the fallback template and create a separate template for this page, where we will display the gallery.

  1. We set the link field for the Gallery page to 'gallery', now our page is available on request /gallery.
  2. In the config/routes.php file, add a new line and thereby redirect the /gallery request to a new template.

    $mvFrontendRoutes = [
    
        '/' => 'view-index.php',
        'e404' => 'view-404.php',
        'fallback' => 'view-default.php',
        
        '/gallery' => 'view-gallery.php'
    ];
  3. In the views folder, create a view-gallery.php file with the following content (the first 2 lines are needed if you want to highlight the menu page that the Pages model issues and issue a 404 error if the page does not exist or is disabled).

    <?
    //File views/view-gallery.php
    
    $content = $mv -> pages -> defineCurrentPage($mv -> router);
    $mv -> display404($content);
    include $mv -> views_path.'main-header.php';
    ?>
        <div class="main">
            <h1><? echo $content -> name; ?></h1>
            <div class="gallery">
    
            </div>
        </div>
    <?
    include $mv -> views_path.'main-footer.php';
    ?>
  4. Now, on request /gallery, the view-gallery.php template file is included, in which we can add the gallery itself from another model. Let's take the simplest version of a gallery on MV, which is one field of a simple model.

    //File models/gallery.model.php
    
    class Gallery extends ModelSimple
    {
        protected $name = 'Gallery';
        
        protected $model_elements = [
            ['Gallery images', 'multi_images', 'photos']
        ];
    }
  5. Create a models/gallery.model.php file, run the migration and get the gallery table in the database. Add a new model to the config/models.php file and get the administered Gallery module in the admin panel.
  6. The gallery object is available as $mv -> gallery is an instance of a simple model with a single field photos. Extract photos, create thumbnails and wrap everything in html tags.

    //File views/view-gallery.php
    
    <div class="gallery">
    <?
        $photos = $mv -> gallery -> photos;
        $photos = $mv -> gallery -> extractImages($photos);
    
        foreach($photos as $photo)
        {
            echo '<a href="'.$mv -> root_path.$photo['image'].'">';
            echo $mv -> gallery -> cropImage($photo['image'], 200, 200).'</a>';
        }
    ?>
    </div>
  7. Final view of the file views/view-gallery.php

    <?
    $content = $mv -> pages -> defineCurrentPage($mv -> router);
    $mv -> display404($content);
    include $mv -> views_path.'main-header.php';
    ?>
        <div class="main">
            <h1><? echo $content -> name; ?></h1>
            <div class="gallery">
            <?
                $photos = $mv -> gallery -> photos;
                $photos = $mv -> gallery -> extractImages($photos);
    
                foreach($photos as $photo)
                {
                    echo '<a href="'.$mv -> root_path.$photo['image'].'">';
                    echo $mv -> gallery -> cropImage($photo['image'], 200, 200).'</a>';
                }
            ?>
            </div>
        </div>
    <?
    include $mv -> views_path.'main-footer.php';
    ?>

Previous section

MV object

Next section

Output of data in template
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github