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
dd($str);just before the return statement give you? Also, I think it should bereturn "<?php echo {$str}; ?>";(without the single quotes).