Reputation: 3198
I am trying to create a custom foreach Blade directive. I know I could use the one provided out of the box but I just thought I'd make my own custom one.
My purpose is to create this directive:
@foremails {{ $email->email }} @endforemails
Blade::directive('foremails', function () {
return "<?php foreach($data->contract->emails as $email): ?>";
});
Blade::directive('endforemails', function () {
return "<?php endforeach; ?>";
});
Also I don't want to have to pass an argument like so @foremails('$data') but only have @foremails because I think that looks pretty clean.
My code is giving me an error: Undefined variable: data
If I try to pass a parameter function($data) it gives me an error: Trying to get property 'contract' of non-object
And if I try function ($data->contract->emails) then I get: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')'
Upvotes: 0
Views: 891
Reputation: 106
Maybe this could help you:
https://laracasts.com/discuss/channels/laravel/custom-blade-directive
Seems like you have to excape the variable
Upvotes: 1