Is it possible to render a flat PHP script in symfony2/twig?
I tried their documentations but still get the same result. The PHP script is being rendered as text only for the example I'd like to render:
<?php
echo "Hello world!";
?>
Is it possible to render a flat PHP script in symfony2/twig?
I tried their documentations but still get the same result. The PHP script is being rendered as text only for the example I'd like to render:
<?php
echo "Hello world!";
?>
NOPE! You can't.
And if that were possible would be a nonsense.
However you can manually to enable the PHP Renderer as template engine:
# app/config/config.yml
framework:
# ...
templating:
engines: ['twig', 'php']
So on your controller class:
# src/AppBundle/Controller/HelloController.php
public function indexAction($name)
{
return $this->render(
'AppBundle:Hello:index.html.php',
array('name' => $name)
);
}
And finally on your PHP template file:
<!-- app/Resources/views/Hello/index.html.php -->
<?php $view->extend('AppBundle::layout.html.php') ?>
Hello <?php echo $name ?>!
Well, as a side note you shouldn't to use/enable twig and php template together. The symfony docs stays it can produce " undesirable side effect in your application". So enable one or another, but not both.
Useful links: