Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Layouts alternate implementation, without exceptions (yet).
- Loading branch information
1 parent
8360eff
commit 315dc54
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class Block extends AbstractHelper { | ||
|
||
static function assign($name, $val) { | ||
Layout::block($name); | ||
echo $val; | ||
Layout::blockEnd(); | ||
} | ||
|
||
static function start($name) { | ||
Layout::block($name); | ||
} | ||
|
||
static function end() { | ||
Layout::blockEnd(); | ||
} | ||
|
||
} | ||
|
||
?> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
Library::import('recess.framework.helpers.AbstractHelper'); | ||
|
||
class Layout extends AbstractHelper { | ||
|
||
const DEFAULT_BLOCK = 'body'; | ||
|
||
protected static $extendStack = array(); | ||
|
||
protected static $blockStack = array(); | ||
protected static $blockMap = array(); | ||
|
||
protected static $slotStack = array(); | ||
|
||
protected static $app; | ||
|
||
public static function init($view) { | ||
$response = $view->getResponse(); | ||
self::$app = $response->meta->app; | ||
} | ||
|
||
public static function extend($layout) { | ||
if(!empty(self::$extendStack)) { | ||
die('error: can\'t nest extends'); | ||
} | ||
|
||
array_push(self::$extendStack, $layout); | ||
ob_start(); | ||
} | ||
|
||
public static function block($title) { | ||
if(empty(self::$extendStack)) { | ||
die('error: not extending'); | ||
} | ||
|
||
if(!empty(self::$blockStack)) { | ||
die('error: cant nest blocks'); | ||
} | ||
|
||
array_push(self::$blockStack, $title); | ||
ob_start(); | ||
} | ||
|
||
public static function blockEnd() { | ||
if(empty(self::$blockStack)) { | ||
die('not in a block!'); | ||
} | ||
|
||
$blockName = array_pop(self::$blockStack); | ||
if(!isset(self::$blockMap[$blockName])) { | ||
self::$blockMap[$blockName] = ob_get_clean(); | ||
} else { | ||
ob_end_clean(); | ||
} | ||
} | ||
|
||
public static function slot($title) { | ||
if(!empty(self::$slotStack)) { | ||
die('can\'t nest slots'); | ||
} | ||
|
||
array_push(self::$slotStack, $title); | ||
ob_start(); | ||
} | ||
|
||
public static function slotEnd() { | ||
if(empty(self::$slotStack)) { | ||
die('not in a slot'); | ||
} | ||
|
||
$slotName = array_pop(self::$slotStack); | ||
if(isset(self::$blockMap[$slotName])) { | ||
ob_end_clean(); | ||
echo self::$blockMap[$slotName]; | ||
unset(self::$blockMap[$slotName]); | ||
} else { | ||
ob_end_flush(); | ||
} | ||
} | ||
|
||
public static function extendEnd() { | ||
if(!empty(self::$extendStack)) { | ||
if(!isset(self::$blockMap[Layout::DEFAULT_BLOCK])) { | ||
self::$blockMap[Layout::DEFAULT_BLOCK] = ob_get_clean(); | ||
} else { | ||
ob_end_clean(); | ||
} | ||
|
||
$parent = array_pop(self::$extendStack); | ||
include(self::$app->getViewsDir() . $parent . '.php'); | ||
self::extendEnd(); | ||
} | ||
} | ||
|
||
} | ||
|
||
?> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class Slot extends AbstractHelper { | ||
|
||
static function assign($name, $val = '') { | ||
Layout::slot($name); | ||
echo $val; | ||
Layout::slotEnd(); | ||
} | ||
|
||
static function start($val) { | ||
Layout::slot($val); | ||
} | ||
|
||
static function end() { | ||
Layout::slotEnd(); | ||
} | ||
|
||
} | ||
|
||
?> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
Library::import('recess.framework.views.NativeView'); | ||
Library::import('recess.framework.helpers.view'); | ||
Library::import('recess.framework.helpers.url'); | ||
Library::import('recess.framework.helpers.html'); | ||
|
||
class LayoutView extends NativeView { | ||
/** | ||
* Realizes HTTP's body content based on the Response parameter. Responsible | ||
* for returning content in the format desired. The render method likely uses | ||
* inversion of control which delegates to another method within the view to | ||
* realize the Response. | ||
* | ||
* @param Response $response | ||
* @abstract | ||
*/ | ||
protected function render(Response $response) { | ||
$this->loadHelper( | ||
'recess.framework.helpers.Layout', | ||
'recess.framework.helpers.Block', | ||
'recess.framework.helpers.Slot', | ||
'recess.framework.helpers.url', | ||
'recess.framework.helpers.html'); | ||
parent::render($response); | ||
Layout::extendEnd(); | ||
} | ||
} | ||
?> |