Skip to content

Instantly share code, notes, and snippets.

@shadcn
Created July 2, 2018 12:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadcn/9b640fc0bea1137b24fff81cef6d43f1 to your computer and use it in GitHub Desktop.
Save shadcn/9b640fc0bea1137b24fff81cef6d43f1 to your computer and use it in GitHub Desktop.
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