Created
February 9, 2012 18:09
-
-
Save weierophinney/1781686 to your computer and use it in GitHub Desktop.
Brainstorming for ZF2 view layer
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 | |
// The default strategy would add a listener to Application::dispatch() at high | |
// priority. This would inject a ViewModel instance with the layout template set | |
// as the template, no capture_to value, and a false terminal flag. | |
// The default strategy would also listen to Dispatchable::dispatch() at low | |
// priority; a returned ViewModel instance would be injected into the MvcEvent | |
// as the ViewModel. At this time, we'd also check for a template on the | |
// ViewModel, and if none found, auto-determine it and inject it based on the | |
// RouteMatch. | |
// Returning a feed | |
$viewModel = new ViewModel($varsToSet); | |
$viewModel->setTemplate('blog/feed') | |
->setTerminal(true); | |
// Setting an alternate capture point | |
$viewModel = new ViewModel($varsToSet); | |
$viewModel->setTemplate('sidebar/news') | |
->captureTo('sidebar'); | |
// ... and then injecting this as a child | |
$parent = new ViewModel($mainVars); | |
$parent->addChild($viewModel); | |
// An event on Dispatchable::dispatch could mark the model as terminal -- e.g., | |
// if an XHR request came through | |
if ($request->isXmlHttpRequest()) { | |
$model = $e->getViewModel(); | |
if (0 == count($model)) { | |
// Set as terminal and return | |
$model->setTerminal(true); | |
return; | |
} | |
if (count($model) > 1) { | |
throw new Exception('Inconsistent state; can't have multiple view models with XHR'); | |
} | |
$child = false; | |
foreach ($model as $child) { | |
// Grab the first child | |
break; | |
} | |
// Set child as terminal, and replace event's view model with child | |
$child->setTerminal(true); | |
$e->setViewModel($child); | |
} | |
// rendering loop... | |
if ($model->hasChildren()) { // could be 0 < count($model) ? | |
foreach ($model as $child) { | |
if ($model->isTerminal()) { | |
throw new Exception('Terminal child; inconsistent state'); | |
} | |
$result = render($child); | |
$captureTo = $child->captureTo(); | |
if (!empty($captureTo)) { | |
$model->setVariable($captureTo, $result); | |
} | |
} | |
} | |
$result = render($model); | |
// do something with result... inject to response, trigger event, ?? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment