LC
twig
Back to Snippets

Drupal Twig Field Rendering Patterns

Common patterns for rendering field values in Drupal 8+ Twig templates.

drupaltwigthemingfields
twig
      {# Plain text field #}
{{ content.field_name|render|striptags|trim }}

{# Trim body text to 200 characters #}
{% set text = content.body|render|striptags %}
{{ text|length > 200 ? text|slice(0, 200) ~ '...' : text }}

{# Image field URL #}
{{ file_url(content.field_image['#items'].entity.uri.value) }}

{# Link field — URL and title #}
{{ content.field_link[0]['#url'] }}
{{ content.field_link[0]['#title'] }}

{# Boolean field #}
{% if node.field_active.value %}Active{% endif %}

{# Taxonomy reference #}
{{ node.field_category.entity.label }}

{# Multi-value field loop #}
{% for item in node.field_tags %}
  {{ item.entity.label }}
{% endfor %}

{# Date field #}
{{ node.field_date.value|date('F j, Y') }}

{# Entity reference — linked title #}
{{ content.field_ref.0['#plain_text'] }}

{# Check if field has value #}
{% if node.field_name.value is not empty %}
  {{ content.field_name }}
{% endif %}