Created
March 20, 2014 13:02
-
-
Save danharper/9663278 to your computer and use it in GitHub Desktop.
Declarative Blade Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@usingPerRow(4) | |
@foreach ($images as $image) | |
@incrementPerRow() | |
@whenStartOfRow | |
<tr> | |
@closeStartOfRow | |
<td><img src="{{ $image->url }}"></td> | |
@whenEndOfRow | |
</tr> | |
@closeEndOfRow | |
@endforeach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $i = 0; $perRow = 4; ?> | |
@foreach ($images as $image) | |
<?php $i++; ?> | |
@if ($i % $perRow == 1) | |
<tr> | |
@endif | |
<td><img src="{{ $image->url }}"></td> | |
@if ($i % $perRow == 0) | |
</tr> | |
@endif | |
@endforeach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// @usingPerRow(4) | |
Blade::extend(function($value, $compiler) { | |
$pattern = $compiler->createMatcher('usingPerRow'); | |
$replace = '<?php $currentPerRowCount = 0; $perRowSetting = $2; ?>'; | |
return preg_replace($pattern, '$1'.$replace, $value); | |
}); | |
// incrementPerRow() | |
Blade::extend(function($value, $compiler) { | |
$pattern = $compiler->createOpenMatcher('incrementPerRow'); | |
$replace = '<?php $currentPerRowCount++; ?>'; | |
return preg_replace($pattern, '$1'.$replace, $value); | |
}); | |
// whenStartOfRow | |
Blade::extend(function($value, $compiler) { | |
$pattern = $compiler->createPlainMatcher('whenStartOfRow'); | |
$replace = '<?php if ($currentPerRowCount % $perRowSetting === 1) : ?>'; | |
return preg_replace($pattern, '$1'.$replace, $value); | |
}); | |
// closeStartOfRow | |
Blade::extend(function($value, $compiler) { | |
$pattern = $compiler->createPlainMatcher('closeStartOfRow'); | |
$replace = '<?php endif; ?>'; | |
return preg_replace($pattern, '$1'.$replace, $value); | |
}); | |
// whenEndOfRow | |
Blade::extend(function($value, $compiler) { | |
$pattern = $compiler->createPlainMatcher('whenEndOfRow'); | |
$replace = '<?php if ($currentPerRowCount % $perRowSetting === 0) : ?>'; | |
return preg_replace($pattern, '$1'.$replace, $value); | |
}); | |
// closeEndOfRow | |
Blade::extend(function($value, $compiler) { | |
$pattern = $compiler->createPlainMatcher('closeEndOfRow'); | |
$replace = '<?php endif; ?>'; | |
return preg_replace($pattern, '$1'.$replace, $value); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment