Skip to content

Instantly share code, notes, and snippets.

@arshad

arshad/Value.md

Created Jul 2, 2018
Embed
What would you like to do?
Value module: |pick and |rename_keys example

Value module: |pick and |rename_keys example

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.'
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment