Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $bind parameter to Blade::directive #53279

Merged
merged 2 commits into from
Oct 24, 2024
Merged

Conversation

hossein-zare
Copy link
Contributor

@hossein-zare hossein-zare commented Oct 23, 2024

I submitted the same PR a few days ago, but it was closed with an unrelated response and wasn’t properly reviewed. I was advised by your colleagues to resubmit it and be clearer. my previous PR #53222

Description

Let's say we want to have a custom @extends directive:

// ▪ I have to repeat this everywhere
@extends('templates.MY_TEMPLATE_NAME.layouts.app')

// ▪ Custom Directive
// Automatically detects the current template name and prepends it
@template_extends('layouts.app')

Currently, this is impossible without modifying files in the ./vendor directory. My changes would be lost if I upgraded Laravel.

Solution

$this->footer is a protected property in BladeCompiler::class which is used inside CompilesLayouts::compileExtends()

Default Behavior of the Blade::directive without the $bind param

// Inside AppServiceProvider::boot()
Blade::directive('template_extends', function () {
    // ...
    
    // ❌ Fails
    // $this refers to AppServerProvider
    $this->footer[] = $echo;
    
});

🟢 Setting the $bind param to true

We can create advanced directives now

// Inside AppServiceProvider::boot()
Blade::directive('template_extends', function () {
    // ...
    
    // ✅ Works
    // $this refers to BladeCompiler
    $this->footer[] = $echo;
 
},           true         );

@taylorotwell taylorotwell merged commit 63fb79d into laravel:11.x Oct 24, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants