0

I have a Twig variable that I am using to show an intro section and then the actual form.

Variable

{% set intro = true %}

When true, the intro section shows up, for example below.

{% if intro == true %}
     <div class="Intro Wrapper Row">
        <div class="form-container">
        <div>
            <div>
                <button onclick="{{ intro|false }}">Lets Begin</button>
            </div>
    </div>

{% endif %}
{% if intro == false %}

    <div class="SurveyFormLogin Wrapper Row">
        <div class="form-container">
        </div>
    </div>

in Twig, how can I set the intro to false so that the Intro hides and Survey shows? or is this the best way to approach this in Twig?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
chewie
  • 529
  • 4
  • 17
  • 2
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – DarkBee May 12 '21 at 09:54

1 Answers1

0

Your Twig template is loaded and rendered server-side, that is, the resulting HTML is generated on server-side and then the HTML is sent to the browser via HTTP response (probably) which is then displayed there. So, you intend to make your Twig more dynamic, which means that you need to pass a value to it. Example:

$app["twig"]->addGlobal("intro", $isIntro);

where $isIntro is assumed to be a properly initialized boolean.

EDIT

An example of specifying nonglobal Twig parameter values:

return $this->render('user/notifications.html.twig', [
    // this array defines the variables passed to the template,
    // where the key is the variable name and the value is the variable value
    // (Twig recommends using snake_case variable names: 'foo_bar' instead of 'fooBar')
    'user_first_name' => $userFirstName,
    'notifications' => $userNotifications,
]);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Why do it with a global? Also OP wants to do it on a click event... – DarkBee May 12 '21 at 10:30
  • @DarkBee it's not necessary to make it global. As about the click event, usually the intro section is always displayed to a user until either a certain time period passed or the user dismissed it once. So, presumably, given the nature of the feature the click is to send a request to the server and then the intro should never be displayed for the user. – Lajos Arpad May 12 '21 at 11:09