M
M
magary42016-06-23 10:40:33
symfony
magary4, 2016-06-23 10:40:33

Render fields via snippets?

On many pages, I get an object of type Content into the template and display its fields

<div class="place-for-first-field">
    <!-- тут проверка о том что поле существуюет и что оно не пустое плюс еще какое-то специфическое условие -->
    <div class="list of classes for first field">		
        {{ content.getField('first_field')  . . .  }}
    </div>
</div>

<div class="place-for-second-field">
    <!-- тут тоже проверка о том что поле существуюет и что оно не пустое плюс еще какое-то другое   специфическое условие -->	
    <div class="container">
       <span class="wrap around field2">
       {{ content.getField('second_field')  . . .  }}
       </span>
    </div>
</div>

what is the best practice for this?
to remove these checks and html wrappers from the template and define them depending on the field type

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Kulikovsky, 2016-06-23
@by25

Alternatively, cut these fragments into mini-templates and then include them:

{# fields/_ first_field.html.twig #}

<div class="place-for-first-field">
    <div class="list of classes for first field">		
        {{ content.getField('first_field')  . . .  }}
    </div>
</div>

And inspect into the template:
If the return values ​​are objects and have a fixed behavior (and display) and are constantly reused in different projects, you can create a TwigExtension, but this is more labor intensive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question