Header Small Ahoy!

Understanding View Files

View files in Trongate are designated for presentation code, primarily HTML. These files may also include other types of code, such as JavaScript and CSS. Typically, each module within a Trongate application contains a directory named 'views' to store these files.

The directory structure within a typical Trongate module.
The directory structure within a typical Trongate module.

The views directory, positioned at the same hierarchical level as the Controllers directory within a module, is optional and depends on the module’s purpose. For example, a module designed for data processing tasks, such as handling API requests or performing background data synchronization, typically does not require a views directory since it operates without user interface elements.

Functionality of View Files

Within the Trongate Framework, view files are PHP files that contain primarily presentation code, most notably HTML. These files are typically invoked from controller files and are recommended to avoid complex PHP logic, adhering to the MVC best practices. This separation ensures that business logic remains within controllers, thereby keeping view files clean and focused solely on the user interface, enhancing maintainability and clarity.

View files are loaded through controller methods using Trongate's built-in view() method. For example:

$this->view('welcome');

This command is typically placed within a method of a controller class. When executed, it instructs Trongate to render (in this instance) the welcome.php view file located in the module's views directory, thereby displaying the HTML and any associated presentation content.

Note: Trongate leverages pure PHP for its views instead of adopting separate templating engines like Blade or Twig. This decision aligns with the original purpose of PHP, initially developed by Rasmus Lerdorf as a templating tool for dynamic content management. Today, while PHP has evolved into a comprehensive programming language, Trongate capitalizes on its templating capabilities to enhance performance and reduce the learning curve. This approach not only streamlines development by removing additional abstraction layers but also optimizes execution efficiency, maintaining a focus on robustness and developer productivity.