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.

Loading Templates

In Trongate, templates are loaded from within controllers using the template method. This technique ensures that pages can follow a defined layout while having dynamic content to be inserted as needed.

Basic Syntax

To load a template, use the template method within a controller:

In this example:

  • The first parameter ('public') specifies which method should be invoked from the Templates class
  • The second parameter ($data) is an optional associative array containing variables to be passed into the template

How Templates Work

When you call $this->template(), Trongate looks for a corresponding method in the Templates class. Here's an example of how the Templates class is structured:

The load() method is responsible for:

  • Loading the template file from the templates/views/ directory
  • Making the $data array variables available to the template
  • Handling the rendering of the template content

Trongate includes an internal class named Template (singular), which will be discussed in more detail later in this chapter. However, this is an entirely different class and the two should not be confused.

To be clear, Template and Templates are two separate and distinct PHP classes within the Trongate framework.

  • The Template class is an internal class, within Trongate's 'engine' directory. It is responsible for manages loading and displaying of content within HTML templates.
  • The Templates class is part of a 'templates' module that exists within the root directory. It contains the templates that are to be used within a given Trongate application.

Template File Structure

Template files are PHP files stored in the templates/views/ directory. Usually, most of the content within a template file will be HTML code. However, one of the strengths of PHP is that it can be seamlessly integrated with HTML. Therefore, it's normal for template files to also contain some PHP code, as well as HTML. Here's an example of a template file (named as 'public.php'):

Key points about the template file:

  • The template provides the overall HTML structure for your pages
  • Template::display($data) is where your view content will be injected
  • Template files have access to framework constants like BASE_URL and helper functions like anchor()

Dynamic Template Selection

Trongate allows templates to be dynamically switched at runtime based on context. For example, different templates can be loaded depending on a user's authentication status or access level.

Example: Loading a Template Based on User Level

In this case:

  • Admins see the template defined in admin() method of the Templates class
  • Other authenticated users see the template defined in members_area() method
  • Non-authenticated users are redirected to the login page

Templates provide the structural foundation of your pages, while views contain the specific content that gets injected into these templates.

The relationshp between templates and views is explored in the next section.

×