The Trongate PHP Framework
Documentation
Introduction
Quick Start
Basic Concepts
Understanding Routing
Controllers
Views
Assets
Modules Calling Modules
Parent & Child Modules
Database Operations
Modules within Modules
Templates & Themes
Helpers Explained
Form Handling
Working with Files
The Module Import Wizard
Authorization & Authentication
The API Explorer
Best Practices

Help Improve Our Docs

If you’ve found an error, spotted something missing, or feel a section could be clearer or better explained, we’d love to hear from you. Your feedback helps keep the documentation accurate and useful for everyone.

Please report issues or suggest improvements on GitHub. Community input is invaluable in making the docs stronger.

Not comfortable with GitHub? No problem — you can also get in touch with us directly via our contact form. We welcome all feedback.

How Templates Work With Views

When working with templates in Trongate, it's important to understand how they work with view files, and how data is passed between templates and views. This section explains the mechanisms that templates use to locate, load and inject content from view files.

How Templates Locate View Files

When you call the template() method, Trongate needs to determine which view file to load into the template. This process is governed by two key properties in your $data array:

  • view_module: Tells the template which module contains the view file
  • view_file: Specifies the name of the view file to load

View File Resolution Process

Let's examine how a template locates a view file through a practical example. Consider this URL:

And the corresponding controller code:

In this case:

  • When the template is loaded, it looks for the view file based on view_module
  • Since view_module is set to 'inventory', the template will look in that module's views directory
  • The view file will be loaded from: modules/inventory/views/product_details.php

Default View Module Resolution

If view_module is not specified in the $data array, the template will use the first URL segment to determine where to look for the view file. For example:

In this case, since the URL begins with 'products', the template will look for the view file at:

It's recommended to always specify view_module explicitly in your $data array, even when it matches the URL segment. This makes your code more maintainable and allows for easier implementation of custom routing rules in the future.

Data Passing Between Templates and Views

Templates in Trongate automatically make variables from the $data array available to both the template file and the loaded view file. This happens through an automatic extraction process.

Example: Data Extraction

The template system will make first_name available directly in both the template and view file:

This automatic extraction removes the need for manually unpacking variables from the $data array, making your template and view files cleaner and more intuitive to work with.

Even though $data arrays are automatically extracted, the original array remains available from within the view file or template. This is particularly useful for debugging purposes. For example, if you are working with a view file and you'd like a reminder of what variables have been passed into the view file, you could use Trongate's json() method, like so:

The Template Display Method

Within your template files, use the display() method to specify where the view content should be inserted.

The display() method is a static method that exists within Trongate's internal Template class. Within a template file, this can be invoked with the following line of code:

Below is an example of a template file that uses the display() method to inject view file content into the page.

The Template::display() method:

  • Loads the appropriate view file based on view_module and view_file
  • Makes all $data variables available to the view
  • Injects the rendered view content into the template

This approach to template and view integration allows for a clean separation between your page's structure (defined in templates) and its specific content (defined in views), while maintaining a simple and intuitive way to pass data to both.

×