Skip to content

Commit

Permalink
Layouts alternate implementation, without exceptions (yet).
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Apr 3, 2009
1 parent 8360eff commit 315dc54
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
21 changes: 21 additions & 0 deletions plugins/recess/framework/helpers/Block.class.php
@@ -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();
}

}

?>
97 changes: 97 additions & 0 deletions plugins/recess/framework/helpers/Layout.class.php
@@ -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();
}
}

}

?>
21 changes: 21 additions & 0 deletions plugins/recess/framework/helpers/Slot.class.php
@@ -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();
}

}

?>
28 changes: 28 additions & 0 deletions plugins/recess/framework/views/LayoutView.class.php
@@ -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();
}
}
?>

0 comments on commit 315dc54

Please sign in to comment.