4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Fragment;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\Templating\EngineInterface;
17 use Symfony\Component\HttpKernel\Controller\ControllerReference;
18 use Symfony\Component\HttpKernel\UriSigner;
21 * Implements the Hinclude rendering strategy.
23 * @author Fabien Potencier <fabien@symfony.com>
25 class HIncludeFragmentRenderer extends RoutableFragmentRenderer
27 private $globalDefaultTemplate;
35 * @param EngineInterface|\Twig_Environment $templating An EngineInterface or a \Twig_Environment instance
36 * @param UriSigner $signer A UriSigner instance
37 * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
38 * @param string $charset
40 public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8')
42 $this->setTemplating($templating);
43 $this->globalDefaultTemplate = $globalDefaultTemplate;
44 $this->signer = $signer;
45 $this->charset = $charset;
49 * Sets the templating engine to use to render the default content.
51 * @param EngineInterface|\Twig_Environment|null $templating An EngineInterface or a \Twig_Environment instance
53 * @throws \InvalidArgumentException
55 public function setTemplating($templating)
57 if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof \Twig_Environment) {
58 throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of \Twig_Environment or Symfony\Component\Templating\EngineInterface');
61 $this->templating = $templating;
65 * Checks if a templating engine has been set.
67 * @return bool true if the templating engine has been set, false otherwise
69 public function hasTemplating()
71 return null !== $this->templating;
77 * Additional available options:
79 * * default: The default content (it can be a template name or the content)
80 * * id: An optional hx:include tag id attribute
81 * * attributes: An optional array of hx:include tag attributes
83 public function render($uri, Request $request, array $options = array())
85 if ($uri instanceof ControllerReference) {
86 if (null === $this->signer) {
87 throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
90 // we need to sign the absolute URI, but want to return the path only.
91 $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), strlen($request->getSchemeAndHttpHost()));
94 // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
95 $uri = str_replace('&', '&', $uri);
97 $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
98 if (null !== $this->templating && $template && $this->templateExists($template)) {
99 $content = $this->templating->render($template);
101 $content = $template;
104 $attributes = isset($options['attributes']) && is_array($options['attributes']) ? $options['attributes'] : array();
105 if (isset($options['id']) && $options['id']) {
106 $attributes['id'] = $options['id'];
108 $renderedAttributes = '';
109 if (count($attributes) > 0) {
110 if (PHP_VERSION_ID >= 50400) {
111 $flags = ENT_QUOTES | ENT_SUBSTITUTE;
115 foreach ($attributes as $attribute => $value) {
116 $renderedAttributes .= sprintf(
118 htmlspecialchars($attribute, $flags, $this->charset, false),
119 htmlspecialchars($value, $flags, $this->charset, false)
124 return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
128 * @param string $template
132 private function templateExists($template)
134 if ($this->templating instanceof EngineInterface) {
136 return $this->templating->exists($template);
137 } catch (\InvalidArgumentException $e) {
142 $loader = $this->templating->getLoader();
143 if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
144 return $loader->exists($template);
148 if (method_exists($loader, 'getSourceContext')) {
149 $loader->getSourceContext($template);
151 $loader->getSource($template);
155 } catch (\Twig_Error_Loader $e) {
164 public function getName()