Skip to content

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

2 participants