VOOZH about

URL: https://deepwiki.com/hypervel/support/5.1-views-and-blade-templating

⇱ Views and Blade Templating | hypervel/support | DeepWiki


Loading...
Menu

Views and Blade Templating

Purpose and Scope

This document covers the view rendering and Blade templating system provided by the View and Blade facades. It explains how to render views, compile Blade templates, work with template features like sections and components, and extend the system with custom directives.

For translation and localization features, see Localization. For safely passing PHP data to JavaScript, see JavaScript Integration.


View Rendering Architecture

The view system provides runtime rendering of templates through the View facade, which resolves to Hyperf's FactoryInterface. The rendering flow integrates with Hyperf's ViewEngine component while adding facade-style access.

View Rendering Flow


Sources: src/Facades/View.php1-89 src/Facades/Blade.php1-53


View Factory Methods

The View facade provides multiple methods for creating and rendering views. These methods resolve through the service container to Hyperf's view factory implementation.

View Creation Methods

MethodDescriptionReturn Type
View::make()Create a view from a template nameViewInterface
View::file()Create a view from an absolute file pathViewInterface
View::first()Create a view from the first existing template in an arrayViewInterface
View::exists()Check if a view existsbool

Immediate Rendering Methods

MethodDescriptionReturn Type
View::renderWhen()Render view conditionally if condition is truestring
View::renderUnless()Render view conditionally if condition is falsestring
View::renderEach()Render view for each item in a collectionstring

Sources: src/Facades/View.php10-16

Code Entity Mapping


Sources: src/Facades/View.php85-88


View Data Binding

Views accept data through multiple mechanisms: direct parameters, shared data, and merge data. The view factory manages these data sources and makes them available to templates.

Data Binding Methods

MethodScopeDescription
View::share()GlobalShare data with all views
View::shared()GlobalRetrieve shared data by key
View::getShared()GlobalGet all shared data
make($view, $data)View-specificPass data to specific view instance
make($view, [], $mergeData)View-specificMerge data with existing view data

Sources: src/Facades/View.php18-41


Blade Compilation System

The Blade facade provides access to Hyperf's Blade compiler, which transforms Blade syntax into executable PHP code. The compilation process occurs once per template modification, with compiled output cached for performance.

Blade Compilation Flow


Sources: src/Facades/Blade.php10-37

Blade Compilation Methods

MethodPurpose
Blade::compile()Compile a Blade template file
Blade::compileString()Compile a Blade string without file I/O
Blade::getPath()Get current compilation path
Blade::setPath()Set compilation path
Blade::getCompiledPath()Get compiled file path for source path
Blade::isExpired()Check if compiled cache is stale

Sources: src/Facades/Blade.php10-37


Custom Directives

Blade supports extending the compiler with custom directives. Directives transform Blade syntax into PHP code during compilation.

Directive Registration


Directive Management Methods

MethodPurpose
Blade::directive($name, $handler)Register a custom directive
Blade::getCustomDirectives()Retrieve all registered custom directives
Blade::extend($compiler)Add a custom compiler extension
Blade::getExtensions()Get all compiler extensions

Sources: src/Facades/Blade.php15-30

Conditional Directives

Blade provides conditional directive registration for reusable logic in templates.

MethodPurpose
Blade::if($name, $callback)Register a named conditional
Blade::check($name, ...$parameters)Check if a named conditional passes

Sources: src/Facades/Blade.php17-18


Sections and Layouts

The view system provides a sections mechanism for template inheritance and layout composition. Sections allow child templates to define content blocks that parent templates can yield.

Section Lifecycle


Section Methods

MethodPhaseDescription
View::startSection($name, $content)DefinitionBegin a section definition
View::stopSection($overwrite)DefinitionEnd section, optionally overwriting
View::appendSection()DefinitionEnd section and append to existing
View::yieldSection()DefinitionEnd section and output immediately
View::inject($section, $content)DefinitionInject content into a section
View::yieldContent($section, $default)OutputRender section content
View::parentPlaceholder($section)OutputPlaceholder for parent template content
View::hasSection($name)QueryCheck if section exists
View::sectionMissing($name)QueryCheck if section is missing
View::getSection($name, $default)QueryGet section content
View::getSections()QueryGet all sections
View::flushSections()CleanupClear all sections

Sources: src/Facades/View.php55-66


Components and Slots

The Blade component system enables reusable UI elements with named slots for content injection. Components can be class-based or anonymous.

Component System Architecture


Component Registration Methods

MethodPurpose
Blade::component($class, $alias, $prefix)Register a class component
Blade::components($components, $prefix)Register multiple components
Blade::getClassComponentAliases()Get registered component aliases
Blade::componentNamespace($namespace, $prefix)Register component namespace
Blade::getClassComponentNamespaces()Get registered namespaces
Blade::aliasComponent($path, $alias)Alias an anonymous component
Blade::getComponentAutoload()Get component autoload configuration
Blade::setComponentAutoload($config)Set component autoload configuration

Sources: src/Facades/Blade.php19-26

Component Runtime Methods

MethodPurpose
View::startComponent($view, $data)Begin component rendering
View::startComponentFirst($names, $data)Begin rendering first existing component
View::renderComponent()Complete and render component
View::slot($name, $content)Define a named slot
View::endSlot()Close current slot

Sources: src/Facades/View.php45-49

Component Compilation Methods

MethodPurpose
Blade::newComponentHash($component)Generate unique hash for component instance
Blade::compileClassComponentOpening()Compile component opening tag
Blade::compileEndComponentClass()Compile component closing tag
Blade::sanitizeComponentAttribute($value)Sanitize attribute values for XSS prevention
Blade::withoutComponentTags()Disable component tag compilation

Sources: src/Facades/Blade.php35-41


Includes

Blade provides include directives for embedding template fragments. Includes can be aliased for convenience.

MethodPurpose
Blade::include($path, $alias)Register an include alias
Blade::aliasInclude($path, $alias)Alias an include path

Sources: src/Facades/Blade.php27-28


Loops and Iteration

The view system tracks loop state for nested iterations, providing access to loop metadata within templates.

Loop State Methods

MethodPurpose
View::addLoop($data)Add a loop to the stack
View::incrementLoopIndices()Increment loop counters
View::popLoop()Remove loop from stack
View::getLastLoop()Get current loop data
View::getLoopStack()Get entire loop stack

Sources: src/Facades/View.php67-71


Stacks

Stacks provide a mechanism for pushing content to named stacks that can be rendered elsewhere in the layout. This is useful for accumulating scripts or styles from components.

Stack Operations


Stack Methods

MethodPurpose
View::startPush($section, $content)Begin pushing content to stack
View::stopPush()End push operation
View::startPrepend($section, $content)Begin prepending content to stack
View::stopPrepend()End prepend operation
View::yieldPushContent($section, $default)Output stack content
View::flushStacks()Clear all stacks

Sources: src/Facades/View.php72-77


View Composers and Creators

Composers and creators allow binding logic to views that executes before rendering. Composers run after the view is instantiated; creators run earlier in the lifecycle.

Composer/Creator Lifecycle


Composer/Creator Methods

MethodPurpose
View::composer($views, $callback)Register a view composer
View::composers($composers)Register multiple composers
View::creator($views, $callback)Register a view creator
View::callComposer($view)Execute composer for view
View::callCreator($view)Execute creator for view

Sources: src/Facades/View.php50-54


Echo Format and Encoding

Blade provides control over how data is echoed in templates, including XSS protection through HTML encoding.

MethodPurpose
Blade::setEchoFormat($format)Set the format for echo statements
Blade::withDoubleEncoding()Enable double encoding for echoed data
Blade::withoutDoubleEncoding()Disable double encoding for echoed data

Sources: src/Facades/Blade.php32-34


View Organization

The view system supports organizing templates through namespaces, locations, and file extensions.

Namespace and Location Management


Organization Methods

MethodPurpose
View::addLocation($location)Add a view file location
View::addNamespace($namespace, $hints)Add a namespace hint
View::prependNamespace($namespace, $hints)Prepend to namespace hints
View::replaceNamespace($namespace, $hints)Replace namespace hints
View::addExtension($extension, $engine, $resolver)Register a view extension
View::getExtensions()Get all registered extensions
View::getFinder()Get the view finder instance
View::setFinder($finder)Set the view finder
View::flushFinderCache()Clear finder cache

Sources: src/Facades/View.php24-35


Precompilers

Precompilers allow preprocessing Blade templates before directive compilation. This enables custom syntax transformations.

MethodPurpose
Blade::precompiler($precompiler)Register a precompiler callable

Sources: src/Facades/Blade.php31


Translation Integration

The view system integrates with the translation system for rendering localized content within templates.

MethodPurpose
View::startTranslation($replacements)Begin capturing translatable content
View::renderTranslation()Render captured translation

Sources: src/Facades/View.php78-79


Rendering State Management

The view system tracks rendering state to prevent issues with nested rendering and to manage once-rendering logic.

MethodPurpose
View::incrementRender()Increment render depth counter
View::decrementRender()Decrement render depth counter
View::doneRendering()Check if all rendering is complete
View::hasRenderedOnce($id)Check if ID has been rendered
View::markAsRenderedOnce($id)Mark ID as rendered
View::flushState()Clear all rendering state
View::flushStateIfDoneRendering()Conditionally flush state

Sources: src/Facades/View.php19-30


Container and Event Integration

The view system integrates with the service container and event dispatcher for dependency injection and lifecycle hooks.

Integration Methods

MethodPurpose
View::getContainer()Get the service container
View::setContainer($container)Set the service container
View::getDispatcher()Get the event dispatcher
View::setDispatcher($events)Set the event dispatcher
View::getEngineResolver()Get the engine resolver

Sources: src/Facades/View.php32-39


Utility and Compilation Methods

Strip Parentheses

The Blade compiler provides utility for parsing directive expressions.

MethodPurpose
Blade::stripParentheses($expression)Remove outer parentheses from expressions

Sources: src/Facades/Blade.php14

Once Directive Compilation

MethodPurpose
Blade::compileEndOnce()Compile the @endonce directive
Blade::compileEchos($value)Compile all echo statements in value

Sources: src/Facades/Blade.php42-43


Code Entity Reference

View Facade Resolution Chain


Sources: src/Facades/View.php85-88

Blade Facade Resolution Chain


Sources: src/Facades/Blade.php49-52

Refresh this wiki

On this page