1

Am I doing this correctly? In my AppServiceProvider class, I have the following:

public function boot(){ Blade::directive('carousel', function($numberOfItems, $addoffset = null) { $str = ''; for($i = 0; $i < (int)$numberOfItems; $i++){ $str .= '<div>'; if($i == $addoffset){ $str .= Ads::show('255x255'); }else{ $str .= '<a href="" class="promo-255x255" style="background:url(http://clipartzebra.com/images/2/game-images/game-images-0' . ($i + 1) . '.jpg) center;background-size:cover;"></a>'; } $str .= '</div>'; } return "<?php echo '" . $str . "'; ?>"; }); } 

Then in the blade template I have this:

@carousel(6) 

What then gets generated is this:

<?php echo ''; ?> 

I have also tried this:

public function boot(){ Blade::directive('carousel', function($numberOfItems, $addoffset = null) { return "<?php for($i = 0; $i < (int)$numberOfItems; $i++){ echo '<div>'; if($i == $addoffset){ echo Ads::show('255x255'); }else{ echo '<a href=\"\" class=\"promo-255x255\" style=\"background:url(http://clipartzebra.com/images/2/game-images/game-images-0' . ($i + 1) . '.jpg) center;background-size:cover;\"></a>'; } echo '</div>'; } ?>"; }); } 

I then get

Undefined variable: i 
3
  • Are you sure you removed compiled Blade files before testing changes? Commented Dec 9, 2015 at 18:21
  • Yes, I have removed the cached files. Commented Dec 9, 2015 at 18:22
  • What does dd($str); just before the return statement give you? Also, I think it should be return "<?php echo {$str}; ?>"; (without the single quotes). Commented Dec 9, 2015 at 18:32

2 Answers 2

4

The reason why it's not working is because the argument that is being passed is (6) rather than the string '6' (note: it includes the parenthesis). If you try to cast that as an int, php will convert it into 0 so the for loop never actually loops through anything.

What you can do is this:

Blade::directive('carousel', function($numberOfItems, $addoffset = null) { $numberOfItems = substr($numberOfItems, 1, -1); ... 

This will remove the first and last characters (aka the parenthesis).

Edit: Also, as an added note, if you look inside the BladeCompiler class, you can see that Laravel uses this tidbit of code quite often:

if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } 
Sign up to request clarification or add additional context in comments.

3 Comments

I've just noticed that. It's a bug, isn't it ?
Ahh, okay, Seems kinda dumb to pass (6) as a parameter... I would think that it would pass each parameter as its own parameter from blade.
I wouldn't say it's a bug since most of the blade directives actually require the parenthesis. Maybe someone could think of a better way of handling this though. :)
1

I don't think you can call a function in a blade template in this way, Also you can not call functions which are in controllers or any other place directly. You can well call any function you need in a blade template, but for this you need to create an helper.php file where you can store your function and than make it available to anywhere you need. Register the helper in the composer.json

"autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/" }, "files": [ "app/Http/helpers.php" ] }, 

THis suppose that your helper.php file is in the app/Http directory.

You can than call any function you have in the helper in any blade template.

2 Comments

A helper function is one way to accomplish that, but it is absolutely possible to extend Blade in the way that OP is trying.
Of course it is, and I agree with you. I was giving him the easy solution. Thanks for pointing this to me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.