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

Commit 3e32f7a

Browse files
committed
Initial template namespace implementation
- Tests and assets created for each template type. - Plates ran out of the box. - Twig updated to use namespacing notation. - zend-view still in progress
1 parent 68ed4f7 commit 3e32f7a

File tree

8 files changed

+95
-1
lines changed

8 files changed

+95
-1
lines changed

src/Template/Twig.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Twig implements TemplateInterface
2020
{
2121
use ArrayParametersTrait;
2222

23+
/**
24+
* @var string
25+
*/
26+
private $suffix;
27+
2328
/**
2429
* @var TwigFilesystem
2530
*/
@@ -35,7 +40,7 @@ class Twig implements TemplateInterface
3540
*
3641
* @param TwigEnvironment $template
3742
*/
38-
public function __construct(TwigEnvironment $template = null)
43+
public function __construct(TwigEnvironment $template = null, $suffix = 'html')
3944
{
4045
if (null === $template) {
4146
$template = $this->createTemplate($this->getDefaultLoader());
@@ -50,6 +55,7 @@ public function __construct(TwigEnvironment $template = null)
5055

5156
$this->template = $template;
5257
$this->twigLoader = $loader;
58+
$this->suffix = is_string($suffix) ? $suffix : 'html';
5359
}
5460

5561
/**
@@ -82,6 +88,7 @@ private function getDefaultLoader()
8288
*/
8389
public function render($name, $params = [])
8490
{
91+
$name = $this->normalizeTemplate($name);
8592
$params = $this->normalizeParams($params);
8693
return $this->template->render($name, $params);
8794
}
@@ -115,4 +122,23 @@ public function getPaths()
115122
}
116123
return $paths;
117124
}
125+
126+
/**
127+
* Normalize namespaced template.
128+
*
129+
* Normalizes templates in the format "namespace::template" to
130+
* "@namespace/template".
131+
*
132+
* @param string $template
133+
* @return string
134+
*/
135+
public function normalizeTemplate($template)
136+
{
137+
$template = preg_replace('#^([^:]+)::(.*)$#', '@$1/$2', $template);
138+
if (! preg_match('#\.[a-z]+$#i', $template)) {
139+
return sprintf('%s.%s', $template, $this->suffix);
140+
}
141+
142+
return $template;
143+
}
118144
}

test/Template/PlatesTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,18 @@ public function testCanRenderWithParameterObjects($params, $search)
160160
$content = str_replace('<?=$this->e($name)?>', $search, $content);
161161
$this->assertEquals($content, $result);
162162
}
163+
164+
/**
165+
* @group namespacing
166+
*/
167+
public function testProperlyResolvesNamespacedTemplate()
168+
{
169+
$template = new PlatesTemplate();
170+
$template->addPath(__DIR__ . '/TestAsset/test', 'test');
171+
172+
$expected = file_get_contents(__DIR__ . '/TestAsset/test/test.php');
173+
$test = $template->render('test::test');
174+
175+
$this->assertSame($expected, $test);
176+
}
163177
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Twig Test Namespace</h1>
2+
3+
<p>This template is from the test namespace.</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"test": "twig"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Plates Test Namespace</h1>
2+
3+
<p>This template is from the test namespace.</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>zend-view Test Namespace</h1>
2+
3+
<p>This template is from the test namespace.</p>

test/Template/TwigTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,32 @@ public function testCanRenderWithParameterObjects($params, $search)
143143
$content = str_replace('{{ name }}', $search, $content);
144144
$this->assertEquals($content, $result);
145145
}
146+
147+
/**
148+
* @group namespacing
149+
*/
150+
public function testProperlyResolvesNamespacedTemplate()
151+
{
152+
$template = new TwigTemplate();
153+
$template->addPath(__DIR__ . '/TestAsset/test', 'test');
154+
155+
$expected = file_get_contents(__DIR__ . '/TestAsset/test/test.html');
156+
$test = $template->render('test::test');
157+
158+
$this->assertSame($expected, $test);
159+
}
160+
161+
/**
162+
* @group namespacing
163+
*/
164+
public function testResolvesNamespacedTemplateWithSuffix()
165+
{
166+
$template = new TwigTemplate();
167+
$template->addPath(__DIR__ . '/TestAsset/test', 'test');
168+
169+
$expected = file_get_contents(__DIR__ . '/TestAsset/test/test.js');
170+
$test = $template->render('test::test.js');
171+
172+
$this->assertSame($expected, $test);
173+
}
146174
}

test/Template/ZendViewTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,18 @@ public function testCanPassViewModelForLayoutParameterWhenRendering()
224224
$this->assertContains($content, $result);
225225
$this->assertContains('<title>ALTERNATE LAYOUT PAGE</title>', $result);
226226
}
227+
228+
/**
229+
* @group namespacing
230+
*/
231+
public function testProperlyResolvesNamespacedTemplate()
232+
{
233+
$template = new ZendView();
234+
$template->addPath(__DIR__ . '/TestAsset/test', 'test');
235+
236+
$expected = file_get_contents(__DIR__ . '/TestAsset/test/test.phtml');
237+
$test = $template->render('test::test');
238+
239+
$this->assertSame($expected, $test);
240+
}
227241
}

0 commit comments

Comments
 (0)