Help wanted on initializing a "named" form (plus solution)

I have a page “page_1” with a form “form_1” which uses a custom template which initialises the text input field. This is the most important part of form_1:

title: 'Form page 1'
template: custom_form_template
form:
    name: form_1
    fields:
        -
            name: title
            label: 'Form 1, Title'
            type: text

The custom template is:

<script> console.log({{ form | json_encode | raw }}) </script>

{% set old_title = 'Test' %}

{% set custom_form_data = { title: old_title } %}
<script> console.log({{ custom_form_data | json_encode | raw }}) </script>

{% do form.setAllData(custom_form_data) %}
<script> console.log({{ form.data.toArray | json_encode | raw }}) </script>

{% include "forms/form.html.twig" %}

When I visit page_1 the form is shown and the text input shows “Test”. All is fine.

Form_2 in page_2 basically is identical to form_1 but for completeness sake here it is as well:

title: 'Form page 2'
template: custom_form_template
form:
    name: form_2
    fields:
        -
            name: title
            label: 'Form 2, Title'
            type: text

Then I change the Twig include statement in the custom template to:
{% include "forms/form.html.twig" with { form: forms('form_2') } %}

Page_1 now does not show form_1 but form_2 instead. That is expected.

What I did not expect is that now the initialisation does not take place. The input text field remains empty.

I tried changing the template setting in form_2 to ‘form’ but that didn’t help.

How do I pass the result of form.setAllData() to form_2 ?

The Twig documentation on the include statement states: “Included templates have access to the variables of the active context.”

The solution
From my own experience I know that the best way to learn is trying to explain something new to someone else. When I arrived here while writing this post it suddenly hit me.

As I was looking at with { form: forms('form_2') } I realised that form was (re)set to only the value of forms('forms_2'). So it was logical that the result of applying the method .setAllData() to form before was lost as it were.

The solution then was quickly found:

{% set old_title = 'Test' %}

{% set custom_form_data = { title: old_title } %}

{% set form_2 = forms('form_2') %}

{% do form_2.setAllData(custom_form_data) %}

{% include "forms/form.html.twig" with { form: form_2 } %}

Maybe this will help anyone in a similar situation.