Skip to content

Commit 227ac2d

Browse files
committed
Merge pull request #211 from FriendsOfSymfony/tag-twig
Adding support for tagging from twig
2 parents a9ee771 + 8e2747f commit 227ac2d

File tree

10 files changed

+116
-12
lines changed

10 files changed

+116
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ Changelog
44
1.3.0
55
-----
66

7-
* **2015-05-08** Configured/annotated cache tags on subrequests
8-
(twig `render(controller())`) are no longer ignored. Additionally, it is now
9-
possible to add tags from code before the response object has been created,
10-
by using the TagHandler.
7+
* Configured/annotated cache tags on subrequests (in twig: `render(controller())`)
8+
are no longer ignored. Additionally, it is now possible to add tags from code
9+
before the response object has been created, by using the TagHandler, and from
10+
twig with the `fos_httpcache_tag` function.
11+
1112
If you defined custom services for the `InvalidateTagCommand`, you should
1213
now inject the TagHandler instead of the CacheManager.
1314

1415
**deprecated** `CacheManager::tagResponse` in favor of `TagHandler::addTags`
16+
1517
* **2015-05-08** Added configuration option for custom proxy client (#208)
1618
* Added support for a simple Etag header in the header configuration (#207)
1719

Resources/config/tag_listener.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<argument>%fos_http_cache.tag_handler.header%</argument>
1515
</service>
1616

17+
<service id="fos_http_cache.twig.tag_extension" class="FOS\HttpCacheBundle\Twig\CacheTagExtension">
18+
<argument id="fos_http_cache.handler.tag_handler" type="service"/>
19+
<tag name="twig.extension"/>
20+
</service>
21+
1722
<service id="fos_http_cache.event_listener.tag"
1823
class="%fos_http_cache.event_listener.tag.class%">
1924
<argument type="service" id="fos_http_cache.handler.tag_handler" />

Resources/doc/features/tagging.rst

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ For more information, see :doc:`/reference/configuration/tags`.
3232
Setting and Invalidating Tags
3333
-----------------------------
3434

35-
You can tag responses in three ways: with the cache manager, configuration and
36-
annotations.
35+
You can tag responses in different ways: with the tag handler from PHP or with
36+
a twig function, from configuration or using annotations on controller actions.
3737

38-
Tagging from code
38+
Tagging from Code
3939
~~~~~~~~~~~~~~~~~
4040

4141
Inject the ``TagHandler`` (service ``fos_http_cache.handler.tag_handler``) and
@@ -76,8 +76,31 @@ To invalidate tags, call ``TagHandler::invalidateTags($tags)``::
7676

7777
See the :ref:`Tag Handler reference <tag_handler_addtags>` for full details.
7878

79-
Configuration
80-
~~~~~~~~~~~~~
79+
Tagging from Twig Templates
80+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
81+
82+
In situations where a page is assembled in the templating layer, it can be more
83+
convenient to add tags from inside the template. This works the same way as
84+
with the tag handler and can also be mixed with the other methods:
85+
86+
.. code-block:: jinja
87+
88+
{# template.html.twig #}
89+
{{ fos_httpcache_tag('mytag') }}
90+
{{ fos_httpcache_tag(['tag-one', 'tag-two']) }}
91+
92+
.. hint::
93+
94+
This twig function never outputs anything into the template but is only
95+
called for the side effect of adding the tag to the response header.
96+
97+
.. note::
98+
99+
Tag invalidation from twig would be a strange architecture and is therefore
100+
not supported.
101+
102+
Tagging with Configuration Rules
103+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81104

82105
Alternatively, you can :doc:`configure rules </reference/configuration/tags>`
83106
for setting and invalidating tags:
@@ -97,8 +120,8 @@ Now if a :term:`safe` request matches the criteria under ``match``, the response
97120
will be tagged with ``news``. When an unsafe request matches, the tag ``news``
98121
will be invalidated.
99122

100-
Annotations
101-
~~~~~~~~~~~
123+
Tagging with Controller Annotations
124+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102125

103126
Add the ``@Tag`` annotations to your controllers to set and invalidate tags::
104127

Resources/doc/spelling_word_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ subdomains
99
yaml
1010
invalidator
1111
ETag
12+
templating

Tests/Functional/EventListener/TagSubscriberTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public function testManualTagging()
9595
$this->assertEquals('manual-tag,sub-tag,sub-items,manual-items', $response->headers->get('X-Cache-Tags'));
9696
}
9797

98+
public function testTwigExtension()
99+
{
100+
$client = static::createClient();
101+
102+
$client->request('GET', '/tag_twig');
103+
$response = $client->getResponse();
104+
$this->assertEquals('tag-from-twig', $response->headers->get('X-Cache-Tags'));
105+
}
106+
98107
protected function tearDown()
99108
{
100109
static::createClient()->getContainer()->unmock('fos_http_cache.cache_manager');

Tests/Functional/Fixtures/Controller/TagController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ public function subrequestAction()
6666

6767
return new Response('subrequest');
6868
}
69+
70+
public function twigAction()
71+
{
72+
$this->render('::tag.html.twig');
73+
}
6974
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ fos_httpcache_tag('tag-from-twig') }}

Tests/Functional/Fixtures/app/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ tag_manual:
1616
path: /tag_manual
1717
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TagController::manualAction }
1818

19+
tag_twig:
20+
path: /tag_twig
21+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TagController::twigAction }
22+
1923
invalidation_route:
2024
path: /invalidate/route/{id}
2125
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\InvalidationController::itemAction }

Tests/Unit/DependencyInjection/Compiler/HashGeneratorPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testConfigNoContext()
4343
$config = $this->getBaseConfig();
4444
$this->extension->load(array($config), $container);
4545
$this->userContextListenerPass->process($container);
46-
$this->assertCount(13, $container->getDefinitions());
46+
$this->assertCount(14, $container->getDefinitions());
4747
}
4848

4949
/**

Twig/CacheTagExtension.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Twig;
4+
5+
use FOS\HttpCache\Handler\TagHandler;
6+
7+
/**
8+
* A Twig extension to allow adding cache tags from twig templates.
9+
*/
10+
class CacheTagExtension extends \Twig_Extension
11+
{
12+
/**
13+
* @var TagHandler
14+
*/
15+
private $tagHandler;
16+
17+
public function __construct(TagHandler $tagHandler)
18+
{
19+
$this->tagHandler = $tagHandler;
20+
}
21+
22+
/**
23+
* {@inheritDoc}
24+
*/
25+
public function getFunctions()
26+
{
27+
return array(
28+
new \Twig_SimpleFunction('fos_httpcache_tag', array($this, 'addTag')),
29+
);
30+
}
31+
32+
/**
33+
* Add a single tag or an array of tags to the response.
34+
*
35+
* The tag string is *not* further processed, you can't use a comma
36+
* separated string to pass several tags but need to build a twig array.
37+
*
38+
* Calling this twig function adds nothing to the output.
39+
*
40+
* @param string|array $tag
41+
*/
42+
public function addTag($tag)
43+
{
44+
$this->tagHandler->addTags((array) $tag);
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
public function getName()
51+
{
52+
return 'fos_httpcache_tag_extension';
53+
}
54+
}

0 commit comments

Comments
 (0)