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.

Template Partials

Partials are reusable pieces of code that represent specific sections of a web page. They allow developers to break down complex templates into smaller, more manageable components. Common examples of partials include:

  • Headers
  • Footers
  • Navigation bars
  • Sidebars
  • Login forms

By using partials, you can maintain a consistent design across your application while reducing code duplication.

The use of partials in templates is entirely optional and not required.

While Trongate does support partials, this feature has been included primarily to accommodate developers who are accustomed to their use in other frameworks. We understand that transitioning to alternative methodologies can sometimes be challenging, but rest assured, there are other approaches that can be equally effective and more suited to modern development practices.

For those seeking alternatives to partials, Trongate offers the option to load entire modules directly from view files or templates.

How to Use Partials

In Trongate, partials are loaded using the partial() method. This method allows you to include a partial file from the templates/views/ directory. Here's a basic example:

In this example, the footer.php file located in templates/views/ will be included in the template.

Passing Data to Partials

Partials can receive and display data passed from the calling module. You can pass data to a partial by including a $data array as the second argument:

Inside the partial, all variables in the $data array are automatically extracted and can be accessed directly. For example, if $data['year'] = 2023, you can use $year inside the partial.


Nested Partials

Partials can include other partials, allowing you to create complex, nested structures. For example, a footer.php partial might itself include a social_links.php partial:

This nesting capability makes partials a powerful tool for building modular and reusable templates.

Example: Template Without Partials

Here's an example of a template that does not use partials:

Example: Template With Partials

Here's the same template, but with the footer being loaded as a partial:

Organizing Partials into Subdirectories

If you prefer to organize your partials into subdirectories (e.g., templates/views/partials/), you can do so by specifying the full path when calling Template::partial(). For example:

This would look for templates/views/partials/footer.php.

In Summary

Partials are a powerful feature in Trongate that allow you to break down your templates into reusable components. Whether you're building a simple website or a complex web application, partials can help you maintain a clean and organized codebase. By using Template::partial(), you can easily include global or module-specific partials, pass data to them, and even nest partials within each other.

×