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.

Passing Data Into View Files

Passing data into view files in Trongate involves initializing an associative array within a controller and passing it to the view() method. This array contains variables that can be accessed directly within the view for dynamic content rendering.

Basic Data Passing

The technique for passing data from controller files into view files comprises of the creation of an associative array of key-value pairs. This data array is then passed as the second argument to the view() method:

This example initializes $data with user information and renders the 'welcome' view, where variables like $first_name and $last_name can be accessed directly.

Complex Data Types

You can pass various data types such as arrays, objects, and booleans into view files. For instance, consider passing an array of cities and today's date to a view:

Accessing Passed Data

Within view files, accessed data is available as standard variables. For debugging or development purposes, you can use:

To display structured data in a formatted manner, use Trongate's json() method:

Example: Rendering Dynamic Content

Enhance the greeting() method to pass a name variable into a view file:

In the 'greeting.php' view file, you can directly echo the $name variable:

Output:

Screenshot showing greeting message with a name
Screenshot showing greeting message with a name.

When rendering data from untrusted sources like user inputs, always sanitize outputs to prevent security vulnerabilities. Trongate provides the json() function for safe output encoding, protecting against XSS attacks by properly formatting content for HTML, XML, JSON, JavaScript, and HTML attributes.

×