Blade Capture Directive

ryangjchandler/blade-capture-directive image

Blade Capture Directive stats

Downloads
1.6M
Stars
40
Open Issues
2
Forks
2

View on GitHub →

Create inline partials in your Blade templates with ease.

Blade Capture Directive for Laravel

The blade-capture-directive package by Ryan Chandler enhances Blade, Laravel's templating engine, by introducing a powerful @capture directive. This directive simplifies the creation of inline partials within Blade templates, allowing developers to encapsulate and reuse code snippets without creating separate partial files.

Key Features:

  • Inline Partials Creation: Easily capture and reuse segments of Blade templates.
  • Dynamic Content: Supports arguments within captured blocks, enabling dynamic output.
  • Scope Inheritance: Captured blocks inherit data from the parent scope, simplifying data handling.

Installation:

Install the package via Composer:

composer require ryangjchandler/blade-capture-directive

Usage:

Define a captured block with @capture and end it with @endcapture. Assign the block to a PHP variable which becomes a callable Closure.

@capture($greeting)
Hello, world!
@endcapture
 
{{ $greeting() }}

Advanced Usage:

Pass arguments to modify the output dynamically:

@capture($personalizedGreeting, $name)
Hello, {{ $name }}!
@endcapture
 
{{ $personalizedGreeting('Ryan') }}

Handle multiple arguments and default values:

@capture($customGreeting, $name, $prefix = 'Hello, ')
{{ $prefix }}{{ $name }}!
@endcapture
 
{{ $customGreeting('Ryan') }}
{{ $customGreeting('Taylor', 'Hey, ') }}

Testing:

Run tests using:

composer test

Contributing and Support:

Contributions are welcome, and the project's changelog, security policy, and contribution guidelines can be found in the repository.

Licensing:

Distributed under the MIT License. Full license details are available in the repository.

This package is ideal for developers looking to streamline their Blade views by reducing redundancy and promoting reusability within their Laravel applications.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Ryangjchandler Blade Capture Directive Related Articles

Hooks for Alpine.js image

Hooks for Alpine.js

Read article
Enforce the Disposal of Objects in PHP image

Enforce the Disposal of Objects in PHP

Read article
A Flat-File Database Driver for Eloquent image

A Flat-File Database Driver for Eloquent

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum