Loading a Template
Once you've created a custom template, the next step is to use it in your Trongate application. Loading a template is a straightforward process that allows you to apply your custom layout to any page in your application.
Key Concepts for Loading Templates
- Template Method: Use the template() method to load your custom template.
- Data Array: Pass a
$data
array to your template to include dynamic content.
Example: Loading a Custom Template
Here's a practical example of how to load a custom template from inside a Trongate module.
The code below would be written inside a controller file, within a Trongate module (i.e., a sub-directory of the 'modules' directory).
function display_member_profile() {
$data['member_name'] = 'John Doe';
$this->template('members_area', $data);
}
Understanding The Code
The code sample above uses Trongate's template() method to load a 'members_area' template. The code also passes a $data
array of key / value pairs into the template. Since the $data
array contains a property named as 'member_name', it means that a $member_name
PHP variable will be available and accessible from within the template.
The example presented here demonstrates a basic implementation of template loading. In most real-world scenarios, it's recommended to inject a view file into your template. This approach allows the inner content of the template to change dynamically while maintaining the overall page structure.
The next section of this documentation covers the process of including view files within templates, providing a more flexible and powerful way to structure your application's user interface.