1

I was thinking to get twig outputs so I could use these twig variables on a form.html.twig such as below:

    <div class="row">
        <div class="col-md-6">
            {{ form.name }}
            {{ form.mail }}
            {{ form.subject }}
        </div>
        <div class="col-md-6">
            {{ form.body }}
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            {{ form.submit }}
        </div>
    </div>

Any idea how to get the similar variables via hooks on MYTHEME.theme?

1 Answer 1

1

I found out a solution which I ended with using {{ element.[machine name] }} instead of {{ children }} on my custom template which is cloned by form.html.twig. The result of the code look similar below:

    <div class="row">
        <div class="col-md-6">
            {{ element.name }}
            {{ element.mail }}
            {{ element.subject }}
        </div>
        <div class="col-md-6">
            {{ element.body }}
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            {{ element.submit }}
        </div>
    </div>

If you have other suggestion, I am happy to know! :)

7
  • 1
    Use form--[form-id].html.twig otherwise this template file will apply to every single form which you may not want.
    – No Sssweat
    Commented May 25, 2019 at 21:20
  • Yes, I have own template. Maybe I was not formulated clearly, I have own such as form--[form-id].html.twig which I cloned the template from form.html.twig ;) Commented May 26, 2019 at 15:07
  • This is not always the best approach, see drupal.stackexchange.com/questions/249856/…
    – 4uk4
    Commented May 26, 2019 at 17:29
  • Do you have a better solution? I would like to theme with twig too, any idea? Commented May 26, 2019 at 20:00
  • Yes, see this answer drupal.stackexchange.com/a/245427/47547
    – 4uk4
    Commented May 26, 2019 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.