Skip to content

Add $bind parameter to Blade::directive#53279

Merged
taylorotwell merged 2 commits into
laravel:11.xfrom
hossein-zare:11.x
Oct 24, 2024
Merged

Add $bind parameter to Blade::directive#53279
taylorotwell merged 2 commits into
laravel:11.xfrom
hossein-zare:11.x

Conversation

@hossein-zare
Copy link
Copy Markdown
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
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