Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fetch model from event parameter #4164

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
4 participants
Contributor

juriansluiman commented Apr 1, 2013

The model used for rendering can be modified during the EVENT_RENDERER and EVENT_RENDERER events. If only attributes (like the template) are modified, the current behaviour does not raise problems. However, when the entire view model is swapped out, the current behaviour does raise a problem.

Use Case

A use case for swapping out view models is the principle of a three step view pattern. The <html>, <head> and contents of the <head> is usually the same for every layout you use. If you want to abstract that contents into a "wrapper" layout, you create a new view model, grab the model from the event and use the model from the event as a child. In code:

$model = $e->getModel();

$wrapper = new ViewModel;
$wrapper->setTemplate('layout/bar');
$wrapper->addChild($model);

$e->setModel($wrapper);

An example wrapper:

<?= $this->doctype() ?>

<html lang="en">
<head>
    <meta charset="utf-8">
    <?= $this->headTitle() ?>
    <?= $this->headMeta() ?>
    <?= $this->headLink() ?>
    <?= $this->headScript() ?>
</head>

<body>
    <?= $this->content ?>
    <?= $this->inlineScript() ?>
</body>
</html>

The layout becomes then simpler:

<?php
$this->headTitle('My Website')
     ->setSeparator(' &middot; ')
     ->setAutoEscape(false);

$this->headLink()
     ->appendStylesheet($this->basePath() . '/styles/css/styles.css');

$this->inlineScript()
     ->prependFile('https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
?>

<header><img src="logo.png"></header>

<nav><?= $this->navigation()->menu() ?></nav>

<section>
  <?= $this->content ?>
</section>

<footer>
    Copyright 2013 ACME
</footer>

This PR

You simply cannot accomplish this or perform any other logic with the view model when the new model is not taken from the event. The simple addition makes it all possible :-)

Fetch model from event parameter
The model used for rendering can be modified during the
EVENT_RENDERER and EVENT_RENDERER events. If only attributes
(like the template) are modified, the current behaviour does
not raise problems. However, when the entire view model is
swapped out, the current behaviour raises problems.

A use case for swapping out view models is the principle of a
three step view pattern. The <html>, <head> and contents of the
<head> is usually the same for every layout you use. If you
want to abstract that contents into a "wrapper" layout, you
create a new view model, grab the model from the event and use
the model from the event as a child. In code:

```
$model = $e->getModel();
$model->setCaptureTo('foo');

$wrapper = new ViewModel;
$wrapper->setTemplate('layout/bar');
$wrapper->addChild($model);

$e->setModel($wrapper);
```
Contributor

juriansluiman commented Apr 1, 2013

Follow up, the "use case" is transformed into a module: https://github.com/juriansluiman/SlmThreeStepView

Owner

weierophinney commented Apr 1, 2013

Please provide unit tests. :-)

On Monday, April 1, 2013, Jurian Sluiman wrote:

Follow up, the "use case" is transformed into a module:
https://github.com/juriansluiman/SlmThreeStepView


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/4164#issuecomment-15736336
.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

Contributor

juriansluiman commented Apr 2, 2013

@weierophinney was working on it, though I had phpunit 3.6.10 and I was suddenly required to use 3.7.* (or: how long did I not contribute?) :-)

Owner

weierophinney commented Apr 2, 2013

@juriansluiman We moved to 3.7 before we even went stable! Welcome back!

Contributor

juriansluiman commented Apr 3, 2013

@weierophinney the commit > comment order is suggesting there is more to come. In fact I made the commit, then posted the comment and then did the push, but Github put the commit above the comment. :)

TL;DR: For me it's good to merge now :)

Contributor

prolic commented Apr 9, 2013

Good to merge for me, too. Many thanks.

Contributor

radnan commented Apr 9, 2013

This is more of a note on the module @juriansluiman posted.

The EVENT_RENDERER and EVENT_RENDERER_POST seem to be only concerned with the view renderer strategy. Not sure you'd want to mess with the view models within those events.

The module you posted breaks for a few cases:

  • if you return a terminal ViewModel from the controller action.
  • when you try to use a json renderer strategy.

I think the MvcEvent::EVENT_DISPATCH is a better place to do this than the ViewEvent::EVENT_RENDERER_POST.

Check out the InjectViewModelListener::injectViewModel() method for more details.

Contributor

juriansluiman commented Apr 9, 2013

@radnan there were some issues I came up with. I am not sure what it was anymore, but it required the swapping to take place as late as possible. Furthermore about termination and json/feed models: yes, those are probably bugs right now and must be corrected in future releases. But those can be better discussed at https://github.com/juriansluiman/SlmThreeStepView

weierophinney added a commit that referenced this pull request Apr 12, 2013

weierophinney added a commit that referenced this pull request Apr 12, 2013

@ghost ghost assigned weierophinney Apr 12, 2013

@juriansluiman juriansluiman referenced this pull request in juriansluiman/SlmThreeStepView Jun 21, 2013

Open

Require minimal ZF2.1.5 #1

weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015

Merge pull request zendframework/zendframework#4164 from juriansluima…
…n/hotfix/view-model-event

Fetch model from event parameter

weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015

weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment