Trongate Website Homepage

Passing View Files into Templates

In Trongate, templates serve as the overall structure for your web pages, while view files contain specific content. This guide explains how to pass view files into templates, allowing for dynamic content within a consistent page structure.

Understanding the Process

Consider a standard view file within a module, named greeting.php, which accepts 'name' and 'age' values from a greeting() method.

Example View File

Sample view file
Sample view file: greeting.php

Basic Method for Loading a View

Initially, you might load this view directly:

function greeting() {
    $data['name'] = 'John';
    $data['age'] = 21;
    $this->view('greeting', $data);
}

Steps to Pass a View into a Template

Step 1: Prepare Your Data

Gather the data for your view and specify the view details:

$data['name'] = 'John';
$data['age'] = 21;
$data['view_module'] = 'welcome';  // Your module name
$data['view_file'] = 'greeting';   // Your view file name

Step 2: Call the Template Method

Use the template() method to load your template:

$this->template('public', $data);

The complete greeting() method should now look like this:

function greeting() {
    $data['name'] = 'John';
    $data['age'] = 21;
    $data['view_module'] = 'welcome';
    $data['view_file'] = 'greeting';
    $this->template('public', $data);
}

Declaring Where View File Content Appears

Trongate's 'Template' module features a display() method that can be used to declare where view file content should appear, with a template file.

To declare where rendered view content should appear: Open your template view file (e.g., public.php) and insert this line where you want your view content to appear:

<?= Template::display($data) ?>

Your template file might now look like this:

Template ready to load a module's view file
A template file ready to load a module's view