Views & the view() Helper
How HTML is rendered and how the view() helper works
What Are Views?
In web development, a "view" is the HTML that gets sent to the browser. It's what the user sees.
In DALT.PHP, views are just PHP files that output HTML. They're not special classes or complex templates - just regular PHP files.
The Simplest Example
Here's a controller that renders a view:
// app/Http/controllers/welcome.php
view('welcome.view.php');That's it! Just one function call.
This tells DALT.PHP: "Find the file welcome.view.php and render it."
Where Views Live
DALT.PHP looks for views in two places:
- App views:
resources/views/ - Platform views:
.dalt/resources/views/
So when you call view('welcome.view.php'), it looks for:
resources/views/welcome.view.php(app).dalt/resources/views/welcome.view.php(platform)
Important: App views take priority over the framework! If both exist, your custom app version is used. This allows you to override and customize the .dalt platform views.
The view() Helper Function
Let's look at how view() works:
// framework/Core/functions.php
function view($path, $attributes = [])
{
// 1. Extract attributes into variables
extract($attributes);
// 2. Build file paths
$appView = base_path('resources/views/' . $path);
$daltView = base_path('.dalt/resources/views/' . $path);
// 3. Check App Views first (Your priority!)
if (file_exists($appView)) {
return require $appView;
}
// 4. Check .dalt as a fallback (Platform Views)
if (is_dir(base_path('.dalt')) && file_exists($daltView)) {
return require $daltView;
}
// 5. Not found? Throw error
throw new \RuntimeException("View not found: {$path}");
}Let's break this down step by step.
Step 1: Extract Attributes
extract($attributes);This is a PHP function that turns array keys into variables.
Example:
view('profile.view.php', [
'name' => 'Alice',
'age' => 25
]);Inside profile.view.php, you can now use:
<h1>Hello, <?= htmlspecialchars($name) ?></h1>
<p>Age: <?= $age ?></p>The $name and $age variables were created by extract().
How extract() Works
$attributes = ['name' => 'Alice', 'age' => 25];
extract($attributes);
// Now you have:
// $name = 'Alice'
// $age = 25It's like unpacking a box - each item in the array becomes its own variable.
Step 2: Build File Paths
$appView = base_path('resources/views/' . $path);
$daltView = base_path('.dalt/resources/views/' . $path);This creates the full file paths to check.
Example: if you call view('posts/index.view.php'):
$appView=/path/to/project/resources/views/posts/index.view.php$daltView=/path/to/project/.dalt/resources/views/posts/index.view.php
Step 3: Check App Views First!
if (file_exists($appView)) {
return require $appView;
}This is where your normal application views live. By checking this directory first, you have the power to create a custom file in your app that completely shadows and overrides a .dalt learning platform view.
Step 4: Check Platform Views (Fallback)
if (is_dir(base_path('.dalt')) && file_exists($daltView)) {
return require $daltView;
}If your app doesn't have the view, the framework falls back to checking the .dalt learning component folder.
Step 5: View Not Found
throw new \RuntimeException("View not found: {$path}");If the view doesn't exist in either location, throw an error.
This helps you catch typos: view('welcom.view.php') (missing 'e') will show a clear error.
ELI5: How view() Works
Imagine you're looking for a book:
- You ask for a book (
view('story.view.php')) - Check the main shelf first (
resources/views/) - If not there, check the special platform shelf (
.dalt/resources/views/) - If still not found, say "book not found" (throw error)
The main shelf is for your app. The special shelf (.dalt) is just a fallback for the learning platform.
A Simple View File
Here's what a view file looks like:
<!-- resources/views/welcome.view.php -->
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to DALT.PHP</h1>
<p>This is a simple view file.</p>
</body>
</html>It's just HTML with some PHP mixed in.
Passing Data to Views
You can pass data from controllers to views:
// Controller
$posts = $db->query('SELECT * FROM posts')->get();
view('posts/index.view.php', [
'posts' => $posts
]);Then in the view:
<!-- resources/views/posts/index.view.php -->
<h1>All Posts</h1>
<?php foreach ($posts as $post): ?>
<article>
<h2><?= htmlspecialchars($post['title']) ?></h2>
<p><?= htmlspecialchars($post['body']) ?></p>
</article>
<?php endforeach; ?>The $posts variable is available because we passed it in the $attributes array.
Why Use htmlspecialchars()?
<?= htmlspecialchars($post['title']) ?>This prevents XSS (Cross-Site Scripting) attacks.
If a post title contains:
<script>alert('hacked')</script>Without htmlspecialchars():
- The browser runs the script
- The attacker can steal cookies, redirect users, etc.
With htmlspecialchars():
- The browser shows the text literally
- The script doesn't run
Always escape user-generated content!
The Welcome View Example
Let's look at the actual welcome view in DALT.PHP:
// resources/views/welcome.view.php
<?php if (function_exists('base_path') && is_dir(base_path('.dalt'))): ?>
<p><a href="/learn">Open Course →</a></p>
<?php endif; ?>This checks:
- Does the
base_path()function exist? - Does the
.daltdirectory exist?
If yes, show a link to the course. If no, hide it.
This way:
- With
.daltinstalled → you see "Open Course" - Without
.dalt→ you just see the normal welcome page
View File Naming Convention
DALT.PHP uses .view.php as the file extension:
welcome.view.phplogin.view.phpposts/index.view.php
This makes it clear which files are views (vs controllers or models).
You could also use:
.php(simpler, but less clear).blade.php(if using Laravel's Blade).twig(if using Twig)
DALT.PHP keeps it simple with plain PHP.
What's Good Here
- Views are just PHP files (no new syntax to learn)
- Simple
view()function (easy to understand) - Can pass data easily with
$attributes - You can override platform views by just creating a file in your app views directory
- Clear error messages when views are missing
Design Note
DALT keeps views as plain PHP on purpose:
- No template engine to learn
- No hidden compilation step
- Data is passed as
$attributesfor convenience
If you prefer maximum explicitness, you can pass a single array (example: $data) and read from it inside the view. DALT doesn’t force a single style.
Common Patterns
Including Partials
<!-- resources/views/posts/index.view.php -->
<?php require base_path('resources/views/layouts/header.php'); ?>
<h1>Posts</h1>
<!-- content here -->
<?php require base_path('resources/views/layouts/footer.php'); ?>Conditional Content
<?php if (isset($_SESSION['user'])): ?>
<p>Welcome back, <?= htmlspecialchars($_SESSION['user']['email']) ?></p>
<?php else: ?>
<p><a href="/login">Please log in</a></p>
<?php endif; ?>Loops
<?php foreach ($posts as $post): ?>
<article>
<h2><?= htmlspecialchars($post['title']) ?></h2>
</article>
<?php endforeach; ?>Empty State
<?php if (empty($posts)): ?>
<p>No posts yet.</p>
<?php else: ?>
<?php foreach ($posts as $post): ?>
<!-- post content -->
<?php endforeach; ?>
<?php endif; ?>Security Reminder
Always escape output:
<!-- ✅ SAFE -->
<?= htmlspecialchars($user['name']) ?>
<!-- ❌ DANGEROUS -->
<?= $user['name'] ?>The only exception is when you're intentionally outputting HTML (like a rich text editor), and you've sanitized it properly.
Testing Views
To test if a view works:
- Create the view file in
resources/views/ - Create a controller that calls
view('your-file.view.php') - Add a route to that controller
- Visit the route in your browser
If you see an error like "View not found", check:
- Is the file path correct?
- Is the file in the right directory?
- Did you spell the filename correctly?