Last active
July 12, 2017 18:15
-
-
Save paulredmond/8ba3c45e0374dc1fd775fe91b92b93e9 to your computer and use it in GitHub Desktop.
Example Blade Extension class to group Blade conditionals and directives...
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 interface would be provided by the framework and properly namespaced | |
* This is just an example of what an interface might be like... | |
*/ | |
interface BladeExtension | |
{ | |
public function getDirectives(); | |
public function getConditionals(); | |
} |
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 | |
/** | |
* Example blade extension class for grouping extensions | |
*/ | |
class ExampleBladeExtension implements BladeExtension | |
{ | |
public function getDirectives() | |
{ | |
return [ | |
'truncate' => [$this, 'truncate'] | |
]; | |
} | |
public function getConditionals() | |
{ | |
return [ | |
'prod' => [$this, 'isProduction'] | |
]; | |
} | |
public function isProduction() | |
{ | |
return app()->environment('production'); | |
} | |
/** | |
* @see https://zaengle.com/blog/exploring-laravels-custom-blade-directives | |
*/ | |
public function truncate($expression) | |
{ | |
list($string, $length) = explode(',',str_replace(['(',')',' '], '', $expression)); | |
return "<?php echo e(strlen({$string}) > {$length} ? substr({$string},0,{$length}).'...' : {$string}); ?>"; | |
} | |
} |
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 isn't properly namespaced, just an example service provider | |
* @see https://laravel.com/docs/5.4/providers | |
*/ | |
class MyServiceProvider | |
{ | |
public function register() | |
{ | |
// Service container available to extensions | |
$this->app->bind('ExampleBladeExtension', function ($app) { | |
return new ExampleBladeExtension(); | |
}); | |
$this->app->tag(['ExampleBladeExtension'], 'blade.extension'); | |
} | |
} | |
// Laravel would then grab all these out of the container (not in this file), including packages, | |
// and register them properly with blade 👍 | |
collect($this->app->tagged('blade.extension'))->each(function ($extension) { | |
// register directives with callables - $extension->getDirectives() | |
// register Blade::if() with callables - $extension->getConditionals() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are just some ideas, not real working code. Sometimes I like grouping related extensions .