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;
20 use Twig\Error\LoaderError;
21 use Twig\Loader\ExistsLoaderInterface;
24 * Implements the Hinclude rendering strategy.
26 * @author Fabien Potencier <fabien@symfony.com>
28 class HIncludeFragmentRenderer extends RoutableFragmentRenderer
30 private $globalDefaultTemplate;
38 * @param EngineInterface|Environment $templating An EngineInterface or a Twig instance
39 * @param UriSigner $signer A UriSigner instance
40 * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
41 * @param string $charset
43 public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8')
45 $this->setTemplating($templating);
46 $this->globalDefaultTemplate = $globalDefaultTemplate;
47 $this->signer = $signer;
48 $this->charset = $charset;
52 * Sets the templating engine to use to render the default content.
54 * @param EngineInterface|Environment|null $templating An EngineInterface or an Environment instance
56 * @throws \InvalidArgumentException
58 public function setTemplating($templating)
60 if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof Environment) {
61 throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of Twig\Environment or Symfony\Component\Templating\EngineInterface');
64 $this->templating = $templating;
68 * Checks if a templating engine has been set.
70 * @return bool true if the templating engine has been set, false otherwise
72 public function hasTemplating()
74 return null !== $this->templating;
80 * Additional available options:
82 * * default: The default content (it can be a template name or the content)
83 * * id: An optional hx:include tag id attribute
84 * * attributes: An optional array of hx:include tag attributes
86 public function render($uri, Request $request, array $options = array())
88 if ($uri instanceof ControllerReference) {
89 if (null === $this->signer) {
90 throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
93 // we need to sign the absolute URI, but want to return the path only.
94 $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), strlen($request->getSchemeAndHttpHost()));
97 // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
98 $uri = str_replace('&', '&', $uri);
100 $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
101 if (null !== $this->templating && $template && $this->templateExists($template)) {
102 $content = $this->templating->render($template);
104 $content = $template;
107 $attributes = isset($options['attributes']) && is_array($options['attributes']) ? $options['attributes'] : array();
108 if (isset($options['id']) && $options['id']) {
109 $attributes['id'] = $options['id'];
111 $renderedAttributes = '';
112 if (count($attributes) > 0) {
113 if (\PHP_VERSION_ID >= 50400) {
114 $flags = ENT_QUOTES | ENT_SUBSTITUTE;
118 foreach ($attributes as $attribute => $value) {
119 $renderedAttributes .= sprintf(
121 htmlspecialchars($attribute, $flags, $this->charset, false),
122 htmlspecialchars($value, $flags, $this->charset, false)
127 return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
131 * @param string $template
135 private function templateExists($template)
137 if ($this->templating instanceof EngineInterface) {
139 return $this->templating->exists($template);
140 } catch (\InvalidArgumentException $e) {
145 $loader = $this->templating->getLoader();
146 if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
147 return $loader->exists($template);
151 if (method_exists($loader, 'getSourceContext')) {
152 $loader->getSourceContext($template);
154 $loader->getSource($template);
158 } catch (LoaderError $e) {
167 public function getName()