TemplateController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\Response;
  13. /**
  14. * TemplateController.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class TemplateController extends ContainerAware
  19. {
  20. /**
  21. * Renders a template.
  22. *
  23. * @param string $template The template name
  24. * @param int|null $maxAge Max age for client caching
  25. * @param int|null $sharedAge Max age for shared (proxy) caching
  26. * @param bool|null $private Whether or not caching should apply for client caches only
  27. *
  28. * @return Response A Response instance
  29. */
  30. public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null)
  31. {
  32. if ($this->container->has('templating')) {
  33. $response = $this->container->get('templating')->renderResponse($template);
  34. } elseif ($this->container->has('twig')) {
  35. $response = new Response($this->container->get('twig')->render($template));
  36. } else {
  37. throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
  38. }
  39. if ($maxAge) {
  40. $response->setMaxAge($maxAge);
  41. }
  42. if ($sharedAge) {
  43. $response->setSharedMaxAge($sharedAge);
  44. }
  45. if ($private) {
  46. $response->setPrivate();
  47. } elseif (false === $private || (null === $private && ($maxAge || $sharedAge))) {
  48. $response->setPublic();
  49. }
  50. return $response;
  51. }
  52. }