Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Last active November 8, 2016 22:21
Show Gist options
  • Save pstuifzand/5cfd4299080b38a595a7f6dbb94c6007 to your computer and use it in GitHub Desktop.
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.
@wrapper('wrapper')
<p>content</p>
@endwrapper
<h1>TEST</h1>
<p>before</p>
@child
<p>after</p>
<?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