๐ผ๏ธ Views & Blade-like Syntax in Squehub
Views are the presentation layer of your Squehub application. They separate your business logic from the user interface, allowing you to build clean, maintainable, and reusable templates.
๐๏ธ View Locations & Loading
Squehub supports flexible view loading from multiple directories, enabling modular and maintainable codebases:
- Framework Root Views Directory:
/views/ - Project-Specific Views Directory:
/project/Views/ - Package Views Directories: Each package under
/project/Packages/YourPackage/Views/
Views use the .squehub.php file extension and can be organized into folders like pages/, partials/, or layouts/ to keep your project tidy.
๐ View Directory Structure
Hereโs an example of a typical views folder structure:
views/
โโโ layouts/
โ โโโ simple.squehub.php
โโโ partials/
โ โโโ nav.squehub.php
โโโ pages/
โโโ about.squehub.php
๐ Layout File Example
Layouts define the common page structure โ such as <head>, header, footer, and main
content areas. Child views inherit layouts and fill in specific content sections.
// views/layouts/simple.squehub.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'Simple Page')</title>
</head>
<body>
<header>
<h1>My Website</h1>
@include('partials.nav')
</header>
<main>
@yield('content')
</main>
<footer>
© @year My Website
</footer>
</body>
</html>
๐งฉ The @extends Directive
The @extends directive allows a view to inherit a layout or parent template.
Important: In Squehub, @extends must always be placed at the bottom of your template file.
This ensures sections are defined before the parent layout is processed.
๐ Child View Example
Child views extend layouts and define content for each section. This keeps your views DRY (Donโt Repeat Yourself).
// views/pages/about.squehub.php
@section('title')
About Us
@endsection
@section('content')
<h2>About Our Company</h2>
<p>We build awesome things with Squehub.</p>
<a href="{{ route('home') }}">Back to Home</a>
@endsection
@extends('layouts.simple')
๐ฆ Includes
Use @include to embed reusable partials like headers, footers, or navigation menus.
@include('partials.nav')
๐ Passing Data to @include and @extends
You can pass data explicitly to your included or extended templates by providing an associative array of variables. This enables dynamic and reusable views.
You can optionally pass data to your included or extended templates using associative arrays.
If no variables are passed, Squehub will automatically share all defined view variables with the template, making the second parameter optional.
This feature simplifies view inheritance and keeps your templates clean and dynamic by default.
Example: Passing data to @include
@include('partials.alert', ['type' => 'success', 'message' => 'Operation completed!'])
In views/partials/alert.squehub.php, you can access the passed variables directly:
<div class="alert alert-{{ $type }}">
{{ $message }}
</div>
Example: Passing data to @extends
@extends('layouts.simple', ['pageTitle' => 'Welcome Page'])
@section('title')
{{ $pageTitle }}
@endsection
@section('content')
<p>This is the welcome page content.</p>
@endsection
In the layouts.simple layout, access the variable $pageTitle as usual:
<title>{{ $pageTitle ?? 'Default Title' }}</title>
๐งฉ Sections & Yield
Sections define placeholders inside layouts, and child views fill these placeholders using @section.
The layout renders the sectionโs content via @yield.
@section('title')
My Page Title
@endsection
@section('content')
<h1>Page Content</h1>
@endsection
๐จ Form Handling Example
Use forms with the @csrf directive for protection against CSRF attacks.
<form method="POST" action="/register">
@csrf
<label>Email:</label>
<input type="email" name="email" required>
<label>Password:</label>
<input type="password" name="password" required>
<button type="submit">Register</button>
</form>
๐ Looping Example
Loop through arrays or collections with @foreach and @endforeach.
@foreach ($users as $user)
<p>{{ $user['name'] }} - {{ $user['email'] }}</p>
@endforeach
๐ง Conditional Logic
Write conditionals with @if, @elseif, @else, and
@endif directives.
@if ($user['is_admin'])
<p>Admin Panel Access</p>
@else
<p>Standard User</p>
@endif
๐งฎ Inline PHP
For complex logic or variables, use @php and @endphp to embed PHP code directly.
@php
$totalUsers = count($users);
@endphp
<p>Total users: {{ $totalUsers }}</p>
๐ Date & Time Directives
Squehub offers handy directives for current date and time:
@year, @month, @date, @time, and
@datetime.
<p>© @year Squehub Framework</p>
๐ Route URL Usage
Use the route() helper inside views to generate URLs based on named routes.
This ensures URLs stay consistent even if route patterns change.
<a href="{{ route('admin.dashboard') }}">Admin Dashboard</a>
๐ Notification Messages
Squehub provides a clean way to flash notifications to users using the Notification::flash()
method in your controllers or routes. These notifications can then be displayed in views using the
@hasNotification('type') directive.
To flash a notification, call:
// In your controller or route handler
Notification::flash('success', "Registration successful! A verification code has been sent to your email: {$verificationCode}");
Notification::flash('info', "Registration successful, but failed to send email. Error: {$errorMessage}. Please contact support if the issue persists.");
These notifications are stored in the session and can be retrieved in your views. To display them, use the Blade-like directive:
@hasNotification('success')
<div class="alert alert-success">{{ $message }}</div>
@endhasNotification
@hasNotification('error')
<div class="alert alert-danger">{{ $message }}</div>
@endhasNotification
โ Summary
- Views can be loaded from the framework root
/views/, the project/project/Views/, or any packageโs/project/Packages/YourPackage/Views/directory. - Use the
.squehub.phpfile extension for views. - Define layouts and extend them using
@extends(bottom of the file). Variable passing is optional. - Fill sections with
@sectionand render them via@yieldin layouts. - Embed reusable partials with
@includeโ passing variables is optional. - Control rendering with directives like
@if,@foreach, and inline PHP blocks. - Secure forms with the
@csrfdirective. - Generate URLs using the
route('name')helper for maintainable links. - Flash and display notifications easily using
Notification::flash()and@hasNotification. - Use handy date/time helpers such as
@year,@month, and@datetime.