Last active
November 8, 2016 22:21
-
-
Save pstuifzand/5cfd4299080b38a595a7f6dbb94c6007 to your computer and use it in GitHub Desktop.
Blade directives that allow you to wrap a piece of your template with another template.
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
@wrapper('wrapper') | |
<p>content</p> | |
@endwrapper |
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
<h1>TEST</h1> | |
<p>before</p> | |
@child | |
<p>after</p> |
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 | |
// Adds these two directives to the boot method of a serviceprovider. | |
\Blade::directive('wrapper', function($value) { | |
$value = substr($value, 1, -1); | |
$s = "<?php \$this->wrappers[] = \"$value\"; ?>"; | |
$s .= "<?php ob_start(); ?>"; | |
return $s; | |
}); | |
\Blade::directive('endwrapper', function($value) { | |
$s = "<?php \$child = ob_get_clean();"; | |
$s .= "\$name = array_pop(\$this->wrappers); ?>"; | |
$s .= "<?php \$contents = \$__env->make(\$name, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>"; | |
$s .= "<?php echo preg_replace('/@child/', \$child, \$contents); ?>"; | |
return $s; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment