ZeffMu icon indicating copy to clipboard operation
ZeffMu copied to clipboard

Template identification

Open BinaryKitten opened this issue 11 years ago • 2 comments

Currently when you return a ViewModel with no template, the system tries to use the template "zeff-mu/closure"

This happens for ALL routes and as such prevents view scripts actually being useful.

We need a way for view scripts to be identified a lot easier than the current method.

BinaryKitten avatar Mar 05 '13 16:03 BinaryKitten

Obviously, the default template injection listener of zend-mvc cannot be used here, since it uses the controller class name. My suggestion is to have something like following:

function (/*...*/) {
    return $this->render(__DIR__ . '/templates/test.php', $data);
}

Which would be a helper similar to following:

public function render($path, $data)
{
    $vm = new ViewModel($data);

    // need to customize the view resolver to allow absolute paths here
    $vm->setTemplate($this->someTransformation($path));

    return $vm;
}

Other use cases:

function (/*...*/) {
    $vm = new ViewModel();
    $vm->setTemplate('blah/blubb'); // developers' responsibility

    return $vm;
}
function (/*...*/) {
    return new ViewModel(); // will fail (not our fault)
}
function (/*...*/) {
    return array(); // will fail (not our fault)
}
function (/*...*/) {
    return 'Hello World'; // set response body to "Hello World"
}
function (/*...*/) {
    $response = $this->getResponse();
    // ...
    return $response; // short-circuit, as in ZF2 standard MVC
}

Ocramius avatar Mar 05 '13 16:03 Ocramius

Another way of handling this is (pseudo-code):

function (/*...*/) {
    echo 'Hello World';
}

Then when it is executed:

ob_start();
$controller();
$output = ob_get_contents();
ob_end_clean();

if ('' !== $output) {
    $response->setBody($output);
} else {
    // use controller return value to find out what to do
}

This would allow using the returned value as template name

Ocramius avatar Mar 05 '13 17:03 Ocramius