How to render an SDC component
The component ID combines the provider machine name and component machine name: my_theme:button.
Render props with Twig include()
Use include() when the component only needs props and its slots can stay empty.
{{ include('my_theme:button', {
label: 'Read more',
url: path('entity.node.canonical', {node: node.id}),
variant: 'primary'
}, with_context = false) }}
Populate a slot with Twig embed
Use embed when you need to fill a declared slot. The block name must match the slot name in button.component.yml.
{% embed 'my_theme:button' with {
label: 'Read more',
url: path('entity.node.canonical', {node: node.id}),
variant: 'primary'
} only %}
{% block icon %}
{{ content.field_icon }}
{% endblock %}
{% endembed %}
Render the component from PHP
Drupal 11 also provides a component render element. Use the singular #variant property; #props contains schema-defined values, while #slots accepts render arrays or scalar values.
use Drupal\Core\Url;
/** @var \Drupal\node\NodeInterface $node */
$build['button'] = [
'#type' => 'component',
'#component' => 'my_theme:button',
'#variant' => 'primary',
'#props' => [
'label' => $this->t('Read more'),
'url' => Url::fromRoute('entity.node.canonical', [
'node' => $node->id(),
])->toString(),
],
'#slots' => [
'icon' => [
'#markup' => '→',
],
],
'#attributes' => [
'data-track' => 'read-more',
],
];
With Twig debug enabled, Drupal adds component boundary comments containing the component ID—and the selected variant when one is used—so you can confirm which component produced the markup.