Last active
August 29, 2015 14:05
-
-
Save victorloux/70f20d2a191ab69e53f6 to your computer and use it in GitHub Desktop.
Shorthand conditions for Laravel Blade
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 | |
/** | |
* This is a sort of “inline”, more readable if-then-echo statement | |
* for Blade in Laravel, mainly used for dynamically adding classes | |
* or attributes to HTML elements. | |
* | |
* The syntax is | |
* | |
* (( [classname] if [condition] )) | |
* | |
* Do not put quotes around the class name. Parentheses around the condition are optional. | |
* so for example: | |
* <a href="{{ Route::to('user') }}" class="userlink(( guest if !Auth::check() ))"> </a> | |
* | |
* Will compile to | |
* | |
* <a href="<?php echo Route::to('user') ?>" class="userlink<?php if(!Auth::check()) echo ' guest'; ?>"> </a> | |
* | |
* | |
* Put this somewhere before your view is loaded (I usually create | |
* a file called app/templates.php to store all my Blade extensions | |
* and require it from app/start/global.php, where the filters.php | |
* file is loaded too) | |
*/ | |
Blade::extend(function($view, $compiler) | |
{ | |
return preg_replace('/(\s*)\(\((?:\s)(.+?)(?:\s+if\s?+)(.+?)(?:\s)\)\)(\s*)/', '$1<?php if($3) echo " $2"; ?>$4', $view); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment