Skip to content

TWIG Patterns

apps/docs/src/content/docs/drupal-integration/twig Click to copy
Copied! apps/docs/src/content/docs/drupal-integration/twig

WC-2026 components work naturally in Drupal TWIG templates. This guide covers common patterns for mapping Drupal fields to component attributes.

{# node--article.html.twig #}
<wc-card variant="elevated">
<wc-heading level="2" slot="header">
{{ label }}
</wc-heading>
<div>
{{ content.body }}
</div>
{% if content.field_cta_link %}
<wc-button slot="actions" variant="primary"
href="{{ content.field_cta_link.0['#url'] }}">
{{ content.field_cta_link.0['#title'] }}
</wc-button>
{% endif %}
</wc-card>
Drupal FieldComponent AttributeExample
Text (plain)String attributevariant="{{ field_variant }}"
BooleanBoolean attribute{% if field_featured %}featured{% endif %}
Linkhref attributehref="{{ field_link.0.url }}"
Entity referenceNested componentLoop with {% for item in items %}
Imagesrc attributesrc="{{ file_url(field_image.entity.uri.value) }}"
{# Only render if field has value #}
{% if content.field_alert_type|render|trim %}
<wc-alert variant="{{ content.field_alert_type.0['#markup'] }}">
{{ content.field_alert_message }}
</wc-alert>
{% endif %}
{# Named slots for component composition #}
<wc-card>
<span slot="header">{{ content.field_title }}</span>
<span slot="media">{{ content.field_image }}</span>
{{ content.body }}
<span slot="actions">{{ content.field_cta }}</span>
</wc-card>

See the Pre-Planning: Drupal Integration Guide for comprehensive TWIG patterns.