Skip to content

Commit a467b02

Browse files
author
Denis Brumann
committed
Add TemplateController for even more effortless templating.
1 parent 5320189 commit a467b02

5 files changed

Lines changed: 163 additions & 5 deletions

File tree

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ The bundle can be installed via Composer:
2727

2828
composer require jaem3l/template-bundle
2929

30-
Usage Examples
31-
--------------
30+
When using this bundle inside a Symfony 4 application bootstrapped with Flex,
31+
make sure you have installed the twig-bundle and set it up with its recipe, e.g.
32+
by using `composer req twig`.
33+
34+
Usage examples for Route-annotation
35+
-----------------------------------
3236

3337
<?php
3438

@@ -66,3 +70,38 @@ Usage Examples
6670
];
6771
}
6872
}
73+
74+
Usage examples for TemplateController
75+
-------------------------------------
76+
77+
In your routing you can now rely on this controller for directly rendering a
78+
provided template:
79+
80+
```
81+
# SF3: app/config/routing.yml
82+
# SF4: config/routes.yaml
83+
84+
hello_world:
85+
path: /hello-world
86+
controller: jæm3l\TemplateBundle\Controller\TemplateController::template
87+
defaults:
88+
template: 'Hello World!'
89+
90+
twig_expression:
91+
path: /twig-expression
92+
controller: jæm3l\TemplateBundle\Controller\TemplateController::template
93+
94+
defaults:
95+
template: '{{ 1 + 2 }}'
96+
97+
advanced_template:
98+
path: /advanced-template
99+
controller: jæm3l\TemplateBundle\Controller\TemplateController
100+
defaults:
101+
template: |
102+
{% extends 'base.html.twig' %}
103+
104+
{% block body %}
105+
Hello World!
106+
{% endblock %}
107+
```

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"require": {
2323
"twig/twig": "^2.4",
24-
"sensio/framework-extra-bundle": "^5.1"
24+
"sensio/framework-extra-bundle": "^5.1",
25+
"php": "^7.1"
2526
},
2627
"require-dev": {
2728
"phpunit/phpunit": "^6.5"

composer.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace jæm3l\TemplateBundle\Controller;
4+
5+
use Symfony\Component\HttpFoundation\Response;
6+
use Twig\Environment;
7+
8+
final class TemplateController
9+
{
10+
private $twig;
11+
12+
public function __construct(Environment $twig)
13+
{
14+
$this->twig = $twig;
15+
}
16+
17+
public function templateAction(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response
18+
{
19+
$twigTemplate = $this->twig->createTemplate($template);
20+
21+
$response = new Response($twigTemplate->render([]));
22+
23+
if ($maxAge) {
24+
$response->setMaxAge($maxAge);
25+
}
26+
if ($sharedMaxAge) {
27+
$response->setSharedMaxAge($sharedMaxAge);
28+
}
29+
if ($private) {
30+
$response->setPrivate();
31+
} elseif (false === $private || (null === $private && ($maxAge || $sharedMaxAge))) {
32+
$response->setPublic();
33+
}
34+
35+
return $response;
36+
}
37+
38+
public function __invoke(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response
39+
{
40+
return $this->templateAction($template, $maxAge, $sharedMaxAge, $private);
41+
}
42+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace jæm3l\TemplateBundle\Tests\Controller;
4+
5+
use jæm3l\TemplateBundle\Controller\TemplateController;
6+
use PHPUnit\Framework\TestCase;
7+
use Twig\Environment;
8+
use Twig\Loader\ArrayLoader;
9+
10+
class TemplateControllerTest extends TestCase
11+
{
12+
/**
13+
* @var TemplateController
14+
*/
15+
private $controller;
16+
17+
protected function setUp()
18+
{
19+
parent::setUp();
20+
21+
$loader = new ArrayLoader([]);
22+
$twig = new Environment($loader);
23+
24+
$this->controller = new TemplateController($twig);
25+
}
26+
27+
public function test_it_renders_basic_template()
28+
{
29+
$template = 'Hello World!';
30+
31+
$response = $this->controller->templateAction($template, null, null, null);
32+
33+
$this->assertSame($template, $response->getContent());
34+
$this->assertSame('no-cache, private', $response->headers->get('Cache-Control'));
35+
}
36+
37+
public function test_it_sets_max_age_on_response()
38+
{
39+
$template = 'Hello World!';
40+
41+
$response = $this->controller->templateAction($template, 360, null, null);
42+
43+
$this->assertContains('max-age=360', $response->headers->get('Cache-Control'));
44+
$this->assertContains('public', $response->headers->get('Cache-Control'));
45+
}
46+
47+
public function test_it_sets_shared_max_age_on_response()
48+
{
49+
$template = 'Hello World!';
50+
51+
$response = $this->controller->templateAction($template, null, 360, null);
52+
53+
$this->assertContains('s-maxage=360', $response->headers->get('Cache-Control'));
54+
$this->assertContains('public', $response->headers->get('Cache-Control'));
55+
}
56+
57+
public function test_it_sets_cache_private_on_response()
58+
{
59+
$template = 'Hello World!';
60+
61+
$response = $this->controller->templateAction($template, null, null, true);
62+
63+
$this->assertContains('private', $response->headers->get('Cache-Control'));
64+
}
65+
66+
public function test_it_sets_cache_public_on_response()
67+
{
68+
$template = 'Hello World!';
69+
70+
$response = $this->controller->templateAction($template, null, null, false);
71+
72+
$this->assertContains('public', $response->headers->get('Cache-Control'));
73+
}
74+
}

0 commit comments

Comments
 (0)