Answers.Body: <p>Your example isn't working because <code>block.type</code> is actually a shorthand for <code>block.type.handle</code>. Use the latter instead and it should work. You can tell this is the case by dumping out <code>block.type</code> - it's not a simple string, it's a great big object.</p>
  <p><code>buttonText[block.type.handle]</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-23T22:29:32.970Z
Answers.Text: thanks again, James!
Answers.UserDisplayName: Roi Agneta
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-23T13:21:57.940Z
Answers.LastActivityDate: 2020-07-23T13:21:57.940Z
Answers.UserDisplayName: James Smith
Answers.Body: <p>Thanks @james smith, that did the trick!</p>
  <p>Revised code:</p>
  <pre><code>
          {% set buttonText = {
      &quot;video&quot; : &quot;Watch the Video&quot;,
      &quot;podcast&quot; : &quot;Listen Now&quot;,
      &quot;externalLink&quot; : &quot;Visit Site&quot;,
      }   
  %}
  
  
  {# resource is a Matrix fieldtye #}
  {% set resources = entry.resource.all() %}
  {% for block in resources %}
      {% set text = buttonText[(block.type.handle)] %}
      &lt;div&gt;
          &lt;h2&gt;{{ block.heading }}&lt;/h2&gt;
          &lt;p&gt;{{ block.description }}&lt;/p&gt;
          &lt;a href=&quot;#&quot; class=&quot;button&quot;&gt;{{ text }}&lt;/a&gt;
      &lt;/div&gt;  
  {% endfor %}
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-23T20:20:31.587Z
Answers.Text: good stuff... if you could accept my answer instead of adding your own, that would be appreciated!
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-23T15:04:31.227Z
Answers.LastActivityDate: 2020-07-23T15:04:31.227Z
Answers.UserDisplayName: Roi Agneta
Body: <p>I am trying to use a lookup table technique I saw in a recent Craft Link List post, but getting errors.</p>
  <p>The general objective is to auto-generate button text from an array of possibilities using a Matrix block type as a key. Here is my code:</p>
  <pre><code>    {% set buttonText = {
      &quot;video&quot; : &quot;Watch the Video&quot;,
      &quot;podcast&quot; : &quot;Listen Now&quot;,
      &quot;externalLink&quot; : &quot;Visit Site&quot;,
      }   
  %}
  
  
  {# resource is a Matrix fieldtye #}
  {% set resources = entry.resource.all() %}
  {% for block in resources %}
      {% set text = buttonText[(block.type)] %}
      &lt;div&gt;
          &lt;h2&gt;{{ block.heading }}&lt;/h2&gt;
          &lt;p&gt;{{ block.description }}&lt;/p&gt;
          &lt;a href=&quot;#&quot; class=&quot;button&quot;&gt;{{ text }}&lt;/a&gt;
      &lt;/div&gt;  
  {% endfor %}
  </code></pre>
  <p>The problem is trying to figure out how to use variable as a key in this statement: set text = buttonText[(block.type)], where buttonText is the array and block.type is the key. The value of block.type is correct and matches the key in the array, so I am guessing the syntax is wrong, but can't get it figured out.</p>
  <p>Thanks for any suggestions!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-23T10:30:42.843Z
Comments.Text: How about `attribute` function? More info here: https://stackoverflow.com/a/38771801/10964722
Comments.UserDisplayName: Piotr Pogorzelski
Tags: <hash>
Title: Using a variable for a hash key
ThreadText: # Using a variable for a hash key
  
  (Asked by: username_0 on 2020-07-22)
  
  I am trying to use a lookup table technique I saw in a recent Craft Link List post, but getting errors.
  
  The general objective is to auto-generate button text from an array of possibilities using a Matrix block type as a key. Here is my code:
  
  ```
      {% set buttonText = {
      "video" : "Watch the Video",
      "podcast" : "Listen Now",
      "externalLink" : "Visit Site",
      }   
  %}
  
  {# resource is a Matrix fieldtye #}
  {% set resources = entry.resource.all() %}
  {% for block in resources %}
      {% set text = buttonText[(block.type)] %}
      <div>
          <h2>{{ block.heading }}</h2>
          <p>{{ block.description }}</p>
          <a href="#" class="button">{{ text }}</a>
      </div>  
  {% endfor %}
  
  ```
  
  The problem is trying to figure out how to use variable as a key in this statement: set text = buttonText[(block.type)], where buttonText is the array and block.type is the key. The value of block.type is correct and matches the key in the array, so I am guessing the syntax is wrong, but can't get it figured out.
  
  Thanks for any suggestions!
  
  **Tags:** hash
  
  > **Comments:**
  > **username_1**: How about `attribute` function? More info here: https://stackoverflow.com/a/38771801/10964722  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-07-23. Score: 3 | Accepted answer
  
  Your example isn't working because `block.type` is actually a shorthand for `block.type.handle`. Use the latter instead and it should work. You can tell this is the case by dumping out `block.type` - it's not a simple string, it's a great big object.
  
  `buttonText[block.type.handle]`
  
  > **Comments:**
  > **username_0**: thanks again, username_2!  
  
  ---
  
  ### Answer by username_0 on 2020-07-23. Score: 1
  
  Thanks @username_2 username_2, that did the trick!
  
  Revised code:
  
  ```
  
          {% set buttonText = {
      "video" : "Watch the Video",
      "podcast" : "Listen Now",
      "externalLink" : "Visit Site",
      }   
  %}
  
  {# resource is a Matrix fieldtye #}
  {% set resources = entry.resource.all() %}
  {% for block in resources %}
      {% set text = buttonText[(block.type.handle)] %}
      <div>
          <h2>{{ block.heading }}</h2>
          <p>{{ block.description }}</p>
          <a href="#" class="button">{{ text }}</a>
      </div>  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_2**: good stuff... if you could accept my answer instead of adding your own, that would be appreciated!  
  
  ---
  
  