yamayama yamayama - 14 days ago 5
Twig Question

storing variable in twig?

i am starting to play with twig, but I am very noob at it.
So sorry if the question sounds utterly stupid, but well.. i looked a bit everywhere, and i didn't find anything.

So, how can i store a string input by the user into a variable in twig?

like in my template I have:

<input type="text" name="name"/>


what shall I do to store the "value" of this input into a variable?
Is here direct way to do it?

or shall i do something completely different?

if so, what should I look for?

Answer

You need to pass the variable from your controller back to your template,

<?php
    $twig->render('template.twig', [
         'name' => isset($_POST['name']) ? $_POST['name'] : '',
    ]);

template.twig

<input type="text" name="name" value="{{ name }}" />
Comments