Skip to content

Commit

Permalink
Add TemplateController for even more effortless templating.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Brumann committed Feb 4, 2018
1 parent 5320189 commit a467b02
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 5 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ The bundle can be installed via Composer:

composer require jaem3l/template-bundle

Usage Examples
--------------
When using this bundle inside a Symfony 4 application bootstrapped with Flex,
make sure you have installed the twig-bundle and set it up with its recipe, e.g.
by using `composer req twig`.

Usage examples for Route-annotation
-----------------------------------

<?php

Expand Down Expand Up @@ -66,3 +70,38 @@ Usage Examples
];
}
}

Usage examples for TemplateController
-------------------------------------

In your routing you can now rely on this controller for directly rendering a
provided template:

```
# SF3: app/config/routing.yml
# SF4: config/routes.yaml
hello_world:
path: /hello-world
controller: jæm3l\TemplateBundle\Controller\TemplateController::template
defaults:
template: 'Hello World!'
twig_expression:
path: /twig-expression
controller: jæm3l\TemplateBundle\Controller\TemplateController::template
defaults:
template: '{{ 1 + 2 }}'
advanced_template:
path: /advanced-template
controller: jæm3l\TemplateBundle\Controller\TemplateController
defaults:
template: |
{% extends 'base.html.twig' %}
{% block body %}
Hello World!
{% endblock %}
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"require": {
"twig/twig": "^2.4",
"sensio/framework-extra-bundle": "^5.1"
"sensio/framework-extra-bundle": "^5.1",
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/Controller/TemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace jæm3l\TemplateBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class TemplateController
{
private $twig;

public function __construct(Environment $twig)
{
$this->twig = $twig;
}

public function templateAction(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response
{
$twigTemplate = $this->twig->createTemplate($template);

$response = new Response($twigTemplate->render([]));

if ($maxAge) {
$response->setMaxAge($maxAge);
}
if ($sharedMaxAge) {
$response->setSharedMaxAge($sharedMaxAge);
}
if ($private) {
$response->setPrivate();
} elseif (false === $private || (null === $private && ($maxAge || $sharedMaxAge))) {
$response->setPublic();
}

return $response;
}

public function __invoke(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response
{
return $this->templateAction($template, $maxAge, $sharedMaxAge, $private);
}
}
74 changes: 74 additions & 0 deletions tests/Controller/TemplateControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types = 1);

namespace jæm3l\TemplateBundle\Tests\Controller;

use jæm3l\TemplateBundle\Controller\TemplateController;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class TemplateControllerTest extends TestCase
{
/**
* @var TemplateController
*/
private $controller;

protected function setUp()
{
parent::setUp();

$loader = new ArrayLoader([]);
$twig = new Environment($loader);

$this->controller = new TemplateController($twig);
}

public function test_it_renders_basic_template()
{
$template = 'Hello World!';

$response = $this->controller->templateAction($template, null, null, null);

$this->assertSame($template, $response->getContent());
$this->assertSame('no-cache, private', $response->headers->get('Cache-Control'));
}

public function test_it_sets_max_age_on_response()
{
$template = 'Hello World!';

$response = $this->controller->templateAction($template, 360, null, null);

$this->assertContains('max-age=360', $response->headers->get('Cache-Control'));
$this->assertContains('public', $response->headers->get('Cache-Control'));
}

public function test_it_sets_shared_max_age_on_response()
{
$template = 'Hello World!';

$response = $this->controller->templateAction($template, null, 360, null);

$this->assertContains('s-maxage=360', $response->headers->get('Cache-Control'));
$this->assertContains('public', $response->headers->get('Cache-Control'));
}

public function test_it_sets_cache_private_on_response()
{
$template = 'Hello World!';

$response = $this->controller->templateAction($template, null, null, true);

$this->assertContains('private', $response->headers->get('Cache-Control'));
}

public function test_it_sets_cache_public_on_response()
{
$template = 'Hello World!';

$response = $this->controller->templateAction($template, null, null, false);

$this->assertContains('public', $response->headers->get('Cache-Control'));
}
}

0 comments on commit a467b02

Please sign in to comment.