Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 9bc8b0e

Browse files
committed
Documented namespacing features
1 parent cc67098 commit 9bc8b0e

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
lines changed

doc/book/template/interface.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,34 @@ namespace Zend\Expressive\Template;
1010
interface TemplateInterface
1111
{
1212
/**
13+
* Render a template, optionally with parameters.
14+
*
15+
* Implementations MUST support the `namespace::template` naming convention,
16+
* and allow omitting the filename extension.
17+
*
1318
* @param string $name
1419
* @param array|object $params
1520
* @return string
1621
*/
1722
public function render($name, $params = []);
1823

1924
/**
25+
* Add a template path to the engine.
26+
*
27+
* Adds a template path, with optional namespace the templates in that path
28+
* provide.
29+
*
2030
* @param string $path
2131
* @param string $namespace
2232
*/
2333
public function addPath($path, $namespace = null);
2434

2535
/**
36+
* Add a template path to the engine.
37+
*
38+
* Adds a template path, with optional namespace the templates in that path
39+
* provide.
40+
*
2641
* @return TemplatePath[]
2742
*/
2843
public function getPaths();
@@ -34,13 +49,19 @@ interface TemplateInterface
3449
> Unfortunately, namespace syntax varies between different template engine
3550
> implementations. As an example:
3651
>
37-
> - Plates uses the syntax `namespace::template`
38-
> - Twig uses the syntax `@namespace/template`
39-
> - zend-view does not natively support namespaces; we mimic it using normal
40-
> directory syntax.
52+
> - Plates uses the syntax `namespace::template`.
53+
> - Twig uses the syntax `@namespace/template`.
54+
> - zend-view does not natively support namespaces, though custom resolvers
55+
> can provide the functionality.
4156
>
42-
> As such, it's not entirely possible to have engine-agnostic templates if you
43-
> use namespaces.
57+
> To make different engines compatible, we require implementations to support
58+
> the syntax `namespace::template` (where `namespace::` is optional) when
59+
> rendering. Additionally, we require that engines allow omitting the filename
60+
> suffix.
61+
>
62+
> When using a `TemplateInterface` implementation, feel free to use namespaced
63+
> templates, and to omit the filename suffix; this will make your code portable
64+
> and allow it to use alternate template engines.
4465
4566

4667
## Paths
@@ -53,10 +74,10 @@ the actual template. You may use the `addPath()` method to do so:
5374
$template->addPath('templates');
5475
```
5576

56-
Many template engines further allow *namespacing* templates; when adding a path,
57-
you specify the template *namespace* that it fulfills, and the engine will only
58-
return a template from that path if the namespace provided matches the namespace
59-
for the path.
77+
Template engines adapted for zend-expressive are also required to allow
78+
*namespacing* templates; when adding a path, you specify the template
79+
*namespace* that it fulfills, and the engine will only return a template from
80+
that path if the namespace provided matches the namespace for the path.
6081

6182
```php
6283
// Resolves to a path registered with the namespace "error";
@@ -81,9 +102,13 @@ of a template as the first argument:
81102
$content = $template->render('foo');
82103
```
83104

84-
One key reason to use templates, however, is to dynamically provide data to
85-
inject in the template. You may do so by passing either an associative array or
86-
an object as the second argument to `render()`:
105+
You can specify a namespaced template using the syntax `namespace::template`;
106+
the `template` segment of the template name may use additional directory
107+
separators when necessary.
108+
109+
One key reason to use templates is to dynamically provide data to inject in the
110+
template. You may do so by passing either an associative array or an object as
111+
the second argument to `render()`:
87112

88113
```php
89114
$content = $template->render('message', [

doc/book/template/intro.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ very specific to the project and/or organization.
66

77
We do, however, provide abstraction for templating via the interface
88
`Zend\Expressive\Template\TemplateInterface`, which allows you to write
9-
middleware that is engine-agnostic. In this documentation, we'll detail the
10-
features of this interface, the various implementations we provide, and how you
11-
can configure, inject, and consume templating in your middleware.
9+
middleware that is engine-agnostic. For Expressive, this means:
10+
11+
- All adapters MUST support template namespacing. Namespaces MUST be referenced
12+
using the notation `namespace::template` when rendering.
13+
- Adapters MUST allow rendering templates that omit the extension; they will, of
14+
course, resolve to whatever default extension they require (or as configured).
15+
- Adapters SHOULD allow passing an extension in the template name, but how that
16+
is handled is left up to the adapter.
17+
- Adapters SHOULD abstract layout capabilities. Many templating systems provide
18+
this out of the box, or similar, compatible features such as template
19+
inheritance. This should be transparent to end-users; they should be able to
20+
simply render a template and assume it has the full content to return.
21+
22+
In this documentation, we'll detail the features of this interface, the various
23+
implementations we provide, and how you can configure, inject, and consume
24+
templating in your middleware.
1225

1326
We currently support:
1427

doc/book/template/zend-view.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ $zendView->setResolver($resolver);
5656
$templates = new ZendView($zendView);
5757
```
5858

59+
> ### Namespaced path resolving
60+
>
61+
> Expressive defines a custom zend-view resolver,
62+
> `Zend\Expressive\Template\ZendView\NamespacedPathStackResolver`. This resolver
63+
> provides the ability to segregate paths by namespace, and later resolve a
64+
> template according to the namespace, using the `namespace::template` notation
65+
> required of `TemplateInterface` implementations.
66+
>
67+
> The ZendView adapter ensures that:
68+
>
69+
> - An AggregateResolver is registered with the renderer. If the registered
70+
> resolver is not an AggregateResolver, it creates one and adds the original
71+
> resolver to it.
72+
> - A NamespacedPathStackResolver is registered with the AggregateResolver, at
73+
> a low priority (0), ensuring attempts to resolve hit it later.
74+
>
75+
> With resolvers such as the TemplateMapResolver, you can also resolve
76+
> namespaced templates, mapping them directly to the template on the filesystem
77+
> that matches; adding such a resolver can be a nice performance boost!
78+
5979
## Layouts
6080

6181
Unlike the other supported template engines, zend-view does not support layouts

src/Template/TemplateInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@
1515
interface TemplateInterface
1616
{
1717
/**
18+
* Render a template, optionally with parameters.
19+
*
20+
* Implementations MUST support the `namespace::template` naming convention,
21+
* and allow omitting the filename extension.
22+
*
1823
* @param string $name
1924
* @param array|object $params
2025
* @return string
2126
*/
2227
public function render($name, $params = []);
2328

2429
/**
30+
* Add a template path to the engine.
31+
*
32+
* Adds a template path, with optional namespace the templates in that path
33+
* provide.
34+
*
2535
* @param string $path
2636
* @param string $namespace
2737
*/
2838
public function addPath($path, $namespace = null);
2939

3040
/**
41+
* Retrieve configured paths from the engine.
42+
*
3143
* @return TemplatePath[]
3244
*/
3345
public function getPaths();

0 commit comments

Comments
 (0)