I want to know what is the right way to print with Twig.


node.field_related_articles.entity.get('field_color_theme').value node.field_related_articles.entity.field_color_theme.value

What is the difference? Which one we should use and why?

Example 2:

When we need to access variables through node.field_something and when with content.field_something in node-something.twig.html?

Example 3:

When we are printing plain text in html like:

"< h4>Related articles< /h4>"

How should we print it with twig right way so that if in future we want multilanguage site we don't need to track all plain text in html to change it?

1 Answer 1



->get('field_example') and ->field_example

are the same and so is TWIG

.get('field_example') and .field_example

both get the field object (field item list) from which you can get the field value(s).

This only works for the variable node (or other entities when you are in a different template) and returns the raw values from the database. For example a date field would return a unix timestamp.

In contrast the content variable contains formatted data ready to be rendered. A date field would be rendered as configured in the content type in a human readable format.

So the answer is, you print variables by using content:

{{ content.field_example }}

Use node only when you need to process the raw data in some way where the formatted output won't work, like for example if you compare two dates you want to use a unix time stamp and not the formatted output.

Both methods are language aware, so there shouldn't be any problem from this side.

If you want to translate static text in twig use the |t filter:

<h4>{{ 'Related articles'|t }}</h4>

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.