Skip to main content

View

Views provide various tools that simplify generating the output. Different view types are available (e.g. HTML, CSV, etc.), which are optimized for the respective output format. What is described in this chapter applies to all view types – but the examples are all shown using HTML views. You can find more information about the different view types in the subchapters.

Defining Views​

In contrast to normal class files, views have an extended file extension [type].php, but they are not treated differently by n2n. n2n already provides the types "html", "xml", "csv" and "json". You can also develop missing view types yourself. It is recommended to create a separate namespace for views. So let's define the view hello.html in the namespace atusch\view. To do this, you have to create a file hello.html.php in the folder of the respective namespace:

<?php
$msg = $view->getParam('msg');
?>
<h1>Hello</h1>
<p><?php $html->out($msg) ?></p>

You can invoke a view via ControllerAdapter::forward():

class ExampleController extends ControllerAdapter {

public function index(ParamGet $msg) {
$this->forward('..\view\hello.html', array('msg' => $msg));
}
}

forward() expects the view name relative to the controller namespace as its first parameter. You can also specify the view name with an absolute namespace by prefixing it with a "" (e.g. \atusch\view\hello.html). This would be necessary, for example, if you want to invoke a view of another module. Alternatively, you can specify the view name relative to the module namespace by prefixing it with a "~" (e.g. ~\view\hello.html). Via the second parameter you can pass an array of values, which you can access in the view via $view->getParam().

In the same way you can include further views in views with $view->import():

<div>
<?php $view->import('foo.html', array('param' => 'foo')) ?>
</div>

Templates​

With $view->useTemplate() you can determine the template that should surround your view. Its usage does not differ from $view->import():

<?php
$view->useTemplate('template.html', array('heading' => 'Template Heading'));
?>
<h2>View Heading</h2>
<p>View Content</p>

With $view->importContentView() you can now determine in template.html where the view should be placed.

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1><?php $html->out($view->getParam('heading')) ?></h1>
<div>
<?php $view->importContentView() ?>
</div>
</body>
</html>

Panels​

You can divide the content of your view into different panels, which can be output at different places in templates.

<?php
$view->useTemplate('template.html');
?>

<h2>View Heading</h2>
<p>View Content</p>

<?php $view->panelStart('aside') ?>
<h2>Aside Heading</h2>
<p>Aside Content</p>
<?php $view->panelEnd() ?>

<?php $view->panelStart('aside2') ?>
<h2>Aside 2 Heading</h2>
<p>Aside 2 Content</p>
<?php $view->panelEnd() ?>

Use $view->importPanel() in the template to output the respective panel. Panels are identified by their name.

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Example</h1>
<div>
<?php $view->importContentView() ?>
</div>

<aside>
<?php $view->importPanel('aside') ?>
</aside>

<?php if ($view->hasPanel('aside2')): ?>
<aside>
<?php $view->importPanel('aside2') ?>
</aside>
<?php endif ?>
</body>
</html>

Meta​

In every view the following helpers are provided, which can be accessed via variables:

VariableType
$viewn2n\ui\view\View
$requestn2n\web\http\Request
$responsen2n\web\http\Response

Depending on the view type, these are complemented by further helpers.