Skip to content

Add $bind parameter to allow binding custom Blade directive handler to the BladeCompiler instance. #53222

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

Closed
wants to merge 1 commit into from

Conversation

hossein-zare
Copy link
Contributor

I was attempting to extend the functionality of the @extends directive but encountered issues accessing properties of the BladeCompiler.
With the introduction of the $bind parameter in the directive method, setting it to true allows full access to the BladeCompiler instance. This effectively makes the Blade compiler more flexible, as it now supports directive handlers that can access its protected properties, making it function similarly to a macroable object.

Example:

Blade::directive(name: 'custom_directive', handler: function ($expression) {
    dd(
        class_basename($this)
    ); // prints: BladeCompiler
}, bind: true);

Real Example:

Blade::directive('template_extends', function ($expression) {
    $expression = $this->stripParentheses($expression);

    // ...

    $echo = "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";

    $this->footer[] = $echo;

    return '';
}, true);

@taylorotwell
Copy link
Member

Personally I would rather just make stripParentheses a static method so you can access it without needing this parameter.

@hossein-zare
Copy link
Contributor Author

@taylorotwell
I think you misunderstood why i need to access properties of the BladeCompiler.
I didn't submit this PR for the stripParentheses method.

I need to push the $echo variable to the $this->footer array which is a protected property in the BladeCompiler class.
Please review my PR.

// A custom @extends directive
Blade::directive('template_extends', function ($expression) {
    // ...

    $echo = "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";

    // This is not possible without merging this PR
    $this->footer[] = $echo;

    return '';
}, true);

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