1

I want to have two different templates for the same field on a node template.

For example:

<div class="navigation>{{ content.field_section }}</div>

    // uses paragraph--section.html.twig 
    // Only want to print the titles from paragraph sections

<section>{{ content.field_section }}</section> 
    // uses paragraph--section2.html.twig
    // Want to print the body and titles from paragraph sections

How can I do this? Is there a way to flag one instance or force a specific template?

4
  • Hm, to me it's unclear what you are asking. What conditions should be met to decide wether to take one or the other template? I wonder if you ain't better off with some include.
    – leymannx
    Commented Nov 5, 2018 at 14:42
  • Why not encapsulate this in the Paragraph bundle itself? They certainly have robust twig suggestions.
    – Kevin
    Commented Nov 5, 2018 at 16:05
  • I think your best chance is to clone the field in a node view hook and set a different view mode in the field display settings. This should get you different theme name suggestions and separate caching of both rendered versions of the paragraph. See drupal.stackexchange.com/questions/218144/…
    – 4uk4
    Commented Nov 5, 2018 at 16:31
  • Maybe you can achieve what you're after with some clever code in "Rewrite output" options for that field, you can use twig there.
    – prkos
    Commented Nov 5, 2018 at 21:30

1 Answer 1

-1

You can override existing twig file in preprocess hook. You may check the link https://www.drupal.org/docs/8/theming/twig/working-with-twig-templates.

1
  • This doesn't solve the problem of two different templates for the same field.
    – Sam
    Commented Nov 7, 2018 at 2:18

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.