Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Spiral Twig Integration #40

Merged
merged 1 commit into from
Nov 9, 2022

Conversation

alexander-schranz
Copy link
Contributor

@alexander-schranz alexander-schranz commented Nov 8, 2022

Add Twig Renderer integration into the Spiral Framework.

Sadly there are 2 issues currently:

@alexander-schranz alexander-schranz added Engine: Twig Twig engine and integration Framework: Spiral Spiral integration labels Nov 8, 2022
public function twigRenderer(TwigRenderer $twigRenderer): string
{
return $twigRenderer->render(
'base.twig',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spiral uses a custom Loader, so not able to use base.html.twig only base.twig which .html part is used by Twig autoescape detection. Need to be checked what happens now and why this is the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Twig\Error\SyntaxError] An exception has been thrown during the compilation of a template ("Unable to load view default:base.html, file does not exist.") in "default:base.html". in /tmp/schranz-templating/examples/usages/spiral/vendor/twig/twig/src/Environment.php:521

Comment on lines +19 to +38
// Use a hack to get Twig from the ViewManager as it is not available as an service yet
$viewManager = $container->get(ViewManager::class);

$engines = $viewManager->getEngines();

$twigEngine = null;
foreach ($engines as $engine) {
if ($engine instanceof TwigEngine) {
$twigEngine = $engine;
break;
}
}

if (null === $twigEngine) {
throw new \LogicException(
\sprintf('Expected "%s" to be registered in the view manager.', TwigEngine::class)
);
}

return $twigEngine->getEnvironment(new ViewContext());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very hacky implementation getting Twig as a service view ViewManager and via a Fake ViewContext over twigEngine service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Engine: Twig Twig engine and integration Framework: Spiral Spiral integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant