View Engine
Template rendering and compilation.
Views are .velvet.php files. The engine supports layouts, sections, includes, and namespace resolution.
Basic usage #
return view('home', ['title' => 'Welcome']);
Resolution order #
user/views(overrides)- Namespaced module views
- Fallback namespaces
Namespaces #
Modules can register namespaces. Reference them using namespace::view.name.
return view('blog::posts.index', ['posts' => $posts]);
Register a namespace #
$engine = app('view');
$engine->namespace('blog', '/path/to/blog/views');
Shared data #
Share data across all views:
$engine->share('siteName', 'My Site');
Built-in shared helpers:
| Variable | Description |
|---|---|
$asset |
Callable for asset() helper |
$url |
Callable for tenant_url() helper |
Custom directives #
Register compile-time directives for domain-specific template syntax:
$engine->directive('datetime', function (string $expression) {
return "<?php echo date('Y-m-d H:i', strtotime({$expression})); ?>";
});
See Template Syntax for usage details.
Compilation #
Templates are compiled to PHP and cached under storage/cache/views.
Clear cache #
./velvet cache:clear
Or programmatically:
$engine = app('view');
$engine->clearCache();
Safe rendering #
Use ViewEngine::safe() when rendering untrusted templates. It disables raw output ({!! !!}) and PHP blocks (@php).
$engine = app('view');
$html = $engine->safe($untrustedTemplate, $data);
Note: Safe mode only supports
@if/@elseif/@else/@endifand@foreach/@endforeachdirectives. All other directives (e.g.@include,@extends,@php,@push,@unless, custom directives) are silently ignored.