Last active
February 25, 2018 18:45
-
-
Save nicooprat/2fbdfbf08344a820097cc62166772136 to your computer and use it in GitHub Desktop.
Simple PHP block yielding system
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 | |
function block($slug, $params = array()) { | |
global $_SLUG_; | |
global $_PARAMS_; | |
$_SLUG_ = $slug; | |
$_PARAMS_ = $params; | |
ob_start(); | |
} | |
function endblock() { | |
global $_SLUG_; | |
global $_PARAMS_; | |
$yield = ob_get_clean(); | |
if(!isset($yield) || empty($yield)) $yield = ''; | |
foreach($_PARAMS_ as $key => $value) { | |
$$key = $value; | |
} | |
$path = dirname(__FILE__) . '/' . $_SLUG_; | |
require($path); | |
unset($GLOBALS['_SLUG_']); | |
unset($GLOBALS['_PARAMS_']); | |
} | |
?> |
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
<div class="<?php echo $class ?>"> | |
<?php echo $yield; ?> | |
</div> |
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 block('templates/child.php', array('class' => 'first')); ?> | |
<p>Child 1</p> | |
<?php endblock(); ?> | |
<?php block('templates/child.php', array('class' => 'second')); ?> | |
<p>Child 2</p> | |
<?php endblock(); ?> |
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
<div class="first"> | |
<p>Child 1</p> | |
</div> | |
<div class="second"> | |
<p>Child 2</p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be sure your
php.ini
hasoutput_buffering = On
.