0

I am trying to work on a project that uses Twig Template. Right now, I am learning it and have checked if I can do any alternate for the project as I know how the MVC architecture works. The Twig Template is different as it has file extension html.twig and the following in it:

{% for user in users %}
    <li>{{ user.username | e }}</li>
{% endfor %} 

Now for alternate purpose, I've created a controller and view (Not using Twig rules) in the Template as follows that's very basic:

Controller:

public function index()
{
   //return something
} 

View:

<html>
<head>
   <title>Hello World</title>
</head>
<body>
   <p>This is a sample page.</p>
</body>
</html>

As you can see, I am not using the default architecture for the project (It works) and thinking not to use Twig architecture for the time being. So my question is if I can continue with raw PHP and basic HTML in the Twig Template architecture? Any idea would be appreciated.

user8512043
  • 1,043
  • 12
  • 20

2 Answers2

1

If the view files are being run through Twig, you won't be able to put any PHP statements inside them.

But you can certainly put plain HTML in Twig files, as you've found.

tremby
  • 9,541
  • 4
  • 55
  • 74
  • You got it right @tremby and was expecting this. Thanks but are you sure that I am unable to write PHP statements directly in the `Twig` file? – user8512043 Jan 29 '18 at 04:12
  • If it's running through the Twig interpreter, absolutely. It doesn't allow that. Here's a question about that: https://stackoverflow.com/questions/18665923/call-php-function-from-twig-template – tremby Jan 29 '18 at 04:17
1

You can use php templating with our without twig

framework:
    # ...
    templating:
        engines: ['twig', 'php']

Render controller in php:

<?php echo $view['actions']->render(
    new \Symfony\Component\HttpKernel\Controller\ControllerReference(
        'App\Controller\HelloController::fancy',
        array(
            'name'  => $name,
            'color' => 'green',
        )
    )
) ?>
goto
  • 7,908
  • 10
  • 48
  • 58