Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds stand-alone usage to documentation #36

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions docs/book/application-integration/stand-alone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# Stand-Alone

The view and all view-helpers of laminas-view can also be used stand-alone.

## The View

The examples uses the following directory structure:

* `public`
* `index.php`
* `templates`
* `index.phtml`
* `layout.phtml`

### Basic Example

#### Setup

[Create a renderer, set a resolver for templates](../php-renderer.md#usage)
and initialize the view in `public/index.php`:

```php
// Create template resolver
$templateResolver = new Laminas\View\Resolver\TemplatePathStack([
'script_paths' => [__DIR__ . '/../templates'],
]);

// Create the renderer
$renderer = new Laminas\View\Renderer\PhpRenderer();
$renderer->setResolver($templateResolver);

// Initialize the view
$view = new Laminas\View\View();
$view->getEventManager()->attach(
Laminas\View\ViewEvent::EVENT_RENDERER,
static function () use ($renderer) {
return $renderer;
}
);
```

#### Create View Script

[Create a view script](../view-scripts.md) in `templates/index.phtml`:

```php
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
<?php
/**
* @var Laminas\View\Renderer\PhpRenderer $this
* @var string $headline
*/
?>
<h1><?= $headline ?></h1>
```

#### Create View Model and render Output

Extend the script in `public/index.php` to add a [view model](../quick-start.md):

```php
$viewModel = new Laminas\View\Model\ViewModel(['headline' => 'Example']);
$viewModel->setTemplate('index');

// Set the return type to get the rendered content
$viewModel->setOption('has_parent', true);

echo $view->render($viewModel); // <h1>Example</h1>
```

<details><summary>Show full code example</summary>

```php
<?php

require_once __DIR__ . '/../vendor/autoload.php';

// Create template resolver
$templateResolver = new Laminas\View\Resolver\TemplatePathStack([
'script_paths' => [__DIR__ . '/../templates'],
]);

// Create the renderer
$renderer = new Laminas\View\Renderer\PhpRenderer();
$renderer->setResolver($templateResolver);

// Initialize the view
$view = new Laminas\View\View();
$view->getEventManager()->attach(
Laminas\View\ViewEvent::EVENT_RENDERER,
static function () use ($renderer) {
return $renderer;
}
);

// Create view model
$viewModel = new Laminas\View\Model\ViewModel(['headline' => 'Example']);
$viewModel->setTemplate('index');

// Set the return type to get the rendered content
$viewModel->setOption('has_parent', true);

// Render
echo $view->render($viewModel);
```

</details>

### Example with Layout

#### Add Layout Script

Create a new file `templates/layout.phtml` and add the following content:

```php
<?php
/**
* @var Laminas\View\Renderer\PhpRenderer $this
* @var string $content
*/
?>
<body>
<?= $content ?>
</body>
```

#### Create Layout Model and render Output

Update the script in `public/index.php` to add a view model for layout:

```php
// Create layout model
$layout = new Laminas\View\Model\ViewModel();
$layout->setTemplate('layout');

// Set the return type to get the rendered content
$layout->setOption('has_parent', true);

// Add previous view model as child
$layout->addChild($viewModel);

// Render
echo $view->render($layout);
```

<details><summary>Show full code example</summary>

```php
<?php

require_once __DIR__ . '/../vendor/autoload.php';

// Create template resolver
$templateResolver = new Laminas\View\Resolver\TemplatePathStack([
'script_paths' => [__DIR__ . '/../templates'],
]);

// Create the renderer
$renderer = new Laminas\View\Renderer\PhpRenderer();
$renderer->setResolver($templateResolver);

// Initialize the view
$view = new Laminas\View\View();
$view->getEventManager()->attach(
Laminas\View\ViewEvent::EVENT_RENDERER,
static function () use ($renderer) {
return $renderer;
}
);

// Create view model
$viewModel = new Laminas\View\Model\ViewModel(['headline' => 'Example']);
$viewModel->setTemplate('index');

// Create layout model
$layout = new Laminas\View\Model\ViewModel();
$layout->setTemplate('layout');

// Set the return type to get the rendered content
$layout->setOption('has_parent', true);

// Add previous view model as child
$layout->addChild($viewModel);

// Render
echo $view->render($layout);
```

</details>

## View Helpers

### Setup

Create the renderer:

```php
$renderer = new Laminas\View\Renderer\PhpRenderer();
```

### Using Helper

```php
echo $renderer->doctype(Laminas\View\Helper\Doctype::HTML5); // <!DOCTYPE html>
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ nav:
- Placeholder: helpers/placeholder.md
- Url: helpers/url.md
- "Advanced usage of helpers": helpers/advanced-usage.md
- 'Application Integration':
- 'Stand-Alone': application-integration/stand-alone.md
- Cookbook:
- "Setting module-specific Layouts": cookbook/setting-module-specific-layouts.md
site_name: laminas-view
Expand Down