0

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?

  • 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 Nov 5 '18 at 14:42
  • Why not encapsulate this in the Paragraph bundle itself? They certainly have robust twig suggestions. – Kevin Nov 5 '18 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/… – 4k4 Nov 5 '18 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 Nov 5 '18 at 21:30
-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.

| improve this answer | |
  • This doesn't solve the problem of two different templates for the same field. – Sam Nov 7 '18 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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