Skip to content

Commit

Permalink
Merge pull request #155 from stefandoorn/render-template
Browse files Browse the repository at this point in the history
Add template argument to bitbag_cms_render_block twig function (closes #148)
  • Loading branch information
patrick477 authored Jun 20, 2018
2 parents d850d36 + ecd076e commit 3407816
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 14 additions & 0 deletions spec/Twig/Extension/RenderBlockExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ function it_renders_block(

$this->renderBlock($twigEnvironment, 'bitbag');
}

function it_renders_block_with_template(
BlockResourceResolverInterface $blockResourceResolver,
BlockTemplateResolverInterface $blockTemplateResolver,
BlockInterface $block,
\Twig_Environment $twigEnvironment
): void
{
$blockResourceResolver->findOrLog('bitbag')->willReturn($block);
$blockTemplateResolver->resolveTemplate($block)->shouldNotBeCalled();
$twigEnvironment->render('@BitBagSyliusCmsPlugin/Shop/Block/htmlBlock_other_template.html.twig', ['block' => $block])->willReturn('<div>BitBag Other Template</div>');

$this->renderBlock($twigEnvironment, 'bitbag', '@BitBagSyliusCmsPlugin/Shop/Block/htmlBlock_other_template.html.twig');
}
}
5 changes: 3 additions & 2 deletions src/Twig/Extension/RenderBlockExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@ public function getFunctions(): array
/**
* @param \Twig_Environment $twigEnvironment
* @param string $code
* @param string|null $template
*
* @return string
*
* @throws TemplateTypeNotFound
*/
public function renderBlock(\Twig_Environment $twigEnvironment, string $code): string
public function renderBlock(\Twig_Environment $twigEnvironment, string $code, ?string $template = null): string
{
$block = $this->blockResourceResolver->findOrLog($code);

if (null !== $block) {
$template = $this->blockTemplateResolver->resolveTemplate($block);
$template = $template ?? $this->blockTemplateResolver->resolveTemplate($block);

return $twigEnvironment->render($template, ['block' => $block]);
}
Expand Down

0 comments on commit 3407816

Please sign in to comment.