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.
- We set the link field for the Gallery page to 'gallery', now our page is available on request /gallery.
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' ];
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'; ?>
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'] ]; }
- 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.
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>
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