Blade is Laravel's templating engine that combines PHP with HTML templates. It provides convenient shortcuts for common PHP control structures, template inheritance, and component features while being compiled into plain PHP code for better performance.
Blade Service Injection allows injecting services into views using @inject directive. Example: @inject('metrics', 'App\Services\MetricsService'). The service is resolved from container and available in view.
Form validation errors are available through $errors variable. Common patterns include @error directive for specific fields and $errors->any() to check for any errors. Errors persist for one redirect via session.
Localization uses @lang directive or __() helper for translations. Supports string replacement, pluralization, and JSON language files. Configure locale in config/app.php or change dynamically.
Dynamic components can be rendered using <x-dynamic-component :component="$componentName">. Component name can be determined at runtime. Useful for flexible UIs and plugin systems.
Anonymous components are created without class files using single Blade templates. Stored in resources/views/components. Support props through variables defined at top of template using @props directive.
Components can be autoloaded using package discovery or manual registration in service providers. Support for component aliases, custom paths, and conditional loading based on environment.
The @yield directive displays content of a specified section. It's typically used in layout templates to define places where child views can inject content. It can also specify default content if section is not defined.
Laravel provides the asset() helper function to generate URLs for assets. Assets are stored in the public directory and can be referenced using asset('css/app.css'). For versioning, use mix() with Laravel Mix.
Laravel Mix is a webpack wrapper that simplifies asset compilation. It's configured in webpack.mix.js and provides features for compiling, versioning, and combining CSS/JS. Used with mix() helper in templates.
View Composers bind data to views whenever they're rendered. Registered in service providers using View::composer(). Useful for sharing data across multiple views without passing from controllers.
Components can be organized in subdirectories and namespaced. Configure component namespaces in service provider using Blade::componentNamespace(). Allows package vendors to register component namespaces.
Components support lazy loading using wire:init or defer loading until needed. Useful for performance optimization. Can combine with placeholder loading states and transitions.
Custom if statements are added using Blade::if() in service provider. Can encapsulate complex conditional logic in reusable directives. Example: Blade::if('env', function ($environment) { return app()->environment($environment); });
Variables in Blade templates are displayed using double curly braces syntax {{ $variable }}. The content within braces is automatically escaped to prevent XSS attacks. For unescaped content, use {!! $variable !!}.
Blade template inheritance allows creating a base layout template (@extends) with defined sections (@section) that child templates can override or extend. Child templates use @extends('layout') to inherit and @section to provide content.
Blade provides convenient directives for control structures: @if, @else, @elseif, @endif for conditionals; @foreach, @while for loops; @switch, @case for switch statements. These compile to regular PHP control structures.
Blade comments are written using {{-- comment --}} syntax. Unlike HTML comments, Blade comments are not included in the HTML rendered to the browser, making them useful for developer notes.
Blade Components are reusable template pieces with isolated logic. Created using 'php artisan make:component', they combine a class for logic and a view for markup. Used with x- prefix: <x-alert type='error'>Message</x-alert>.
Stacks in Blade allow pushing content to named locations using @push and @stack directives. Multiple @push calls can add to same stack. Useful for scripts and styles that need to be included at specific points in layout.
Custom Blade directives are created in a service provider using Blade::directive(). They transform template syntax into PHP code. Example: Blade::directive('datetime', function ($expression) { return "<?php echo date('Y-m-d', $expression); ?>"; });
Blade provides @auth and @guest directives for authentication checks. Additional directives like @can, @cannot for authorization. Can be combined with roles/permissions: @can('update', $post).
Attributes Bag ($attributes) manages additional attributes passed to components. Supports merging, filtering, and getting first/last. Example: <div {{ $attributes->merge(['class' => 'default']) }}>.
Custom template compilation can be implemented by extending Blade compiler. Add custom compilation passes, modify existing directives, or add preprocessing steps. Requires understanding of Laravel's compilation process.
Sub-views can be included using @include('view.name') directive. You can also pass data to included views: @include('view.name', ['data' => $data]). For performance, use @includeIf, @includeWhen, or @includeUnless.
Blade supports loops using @foreach, @for, @while directives. The $loop variable is available inside foreach loops, providing information like index, iteration, first, last, and depth for nested loops.
Slots allow passing content to components. Default slot uses <x-slot> tag, named slots use name attribute. Components can have multiple slots and default content. Useful for flexible component layouts.
Component methods can use dependency injection through method parameters. Laravel automatically resolves dependencies from container. Useful for accessing services within component methods.
Components have rendering lifecycle hooks like mount(), rendering(), rendered(). Can modify component state and attributes during render cycle. Useful for complex component behavior.