Given the following component:
<div class="card">
<div class="card-header">
<h4>{{ title }}</h4>
</div>
<div class="card-body">
{{ text }}
</div>
</div>
And an Article node:
$variables['_article'] = [
'nid' => 1,
'title' => 'Minus veritatis rem tenetur hic repellendus fugit.',
'created' => 1538956800,
'field_foo' => 'bar',
'field_teaser' => 'Excepturi veniam dignissimos. Deserunt esse ipsum labore officiis doloremque mollitia. Quibusdam a mollitia numquam laborum dolore.'
];
You can use a combination of |pick
and |rename_keys
filters as follows:
{{ @include('@components/card.twig') with _article|pick(['title', 'field_teaser'])|rename_keys({field_teaser: 'text'}) %}
This will select the title
and field_teaser
values only and rename field_teaser
to text
for the component:
[
'title' => 'Minus veritatis rem tenetur hic repellendus fugit.',
'text' => 'Excepturi veniam dignissimos. Deserunt esse ipsum labore officiis doloremque mollitia. Quibusdam a mollitia numquam laborum dolore.'
];