Created
May 6, 2016 10:35
-
-
Save devudit/10b20f3d299d7c49c73b95e2b2dc2bea to your computer and use it in GitHub Desktop.
Dynamic body classes in laravel
This file contains hidden or 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
"autoload": { | |
"classmap": [ | |
"database", | |
"app/Helper/Helper.php" | |
], | |
"psr-4": { | |
"App\\": "app/" | |
} | |
}, |
This file contains hidden or 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 | |
// Create A class Helper as app/Helper/Helper.php | |
use Illuminate\Support\Facades\Request; | |
Class Helper { | |
public static function getBodyClass() { | |
$body_classes = [ ]; | |
$class = ""; | |
foreach ( Request::segments() as $segment ) { | |
if ( is_numeric( $segment ) || empty( $segment ) ) { | |
continue; | |
} | |
$class .= ! empty( $class ) ? "-" . $segment : $segment; | |
array_push( $body_classes, $class ); | |
} | |
if ( empty($body_classes) ) { | |
$body_classes[] = 'home'; | |
} | |
return ! empty( $body_classes ) ? implode( ' ', $body_classes ) : NULL; | |
} | |
} |
This file contains hidden or 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
<!-- On Boday Add Helper classe function | |
------------- --> | |
<body class="{{ Helper::getBodyClass() }} other-class"> | |
<!-- ------- --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment