Overview
Views in S-PHP handle the presentation layer of your application. TheView class provides a powerful rendering engine with support for layouts, components, and data passing.
Rendering Views
TheView::render() static method is used to display views from your controllers.
Sphp/Core/View.php
Basic Usage
Accessing Data in Views
Data passed to views is automatically extracted using PHP’sextract() function, making variables directly accessible:
app/views/users/show.php
Layout Directive
The@layout directive allows you to include layout files that wrap your content. Layouts are stored in app/views/layout/.
Syntax
Example
app/views/welcome.php
app/views/layout/main.php
How @layout Works
Sphp/Core/View.php
Component Directive
The@component directive includes reusable component files. Components are stored in app/views/components/.
Syntax
Example
app/views/dashboard.php
app/views/components/navbar.php
app/views/components/user-card.php
How @component Works
Sphp/Core/View.php
View File Organization
Organize your views in a logical directory structure:Complete Example
app/Controllers/UserController.php
app/views/users/index.php
Error Handling
When a view file is not found, S-PHP automatically renders the404.html page:
Best Practices
Separate Concerns
Keep business logic in controllers and models. Views should only handle presentation.
Use Layouts
Create reusable layouts for consistent page structure across your application.
Component Reusability
Extract repeated UI elements into components for easier maintenance.
Escape Output
Use
htmlspecialchars() or similar functions to prevent XSS attacks when outputting user data.Security Considerations
Always escape user-generated content:Next Steps
Controllers
Learn how to render views from controllers
Models
Understand how to fetch data for your views