<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>

CC BY-SA 4.0
2020-07-23T22:29:32.970Z
thanks again, James!
Roi Agneta
CC BY-SA 4.0
2020-07-23T13:21:57.940Z
2020-07-23T13:21:57.940Z
James Smith
<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>

CC BY-SA 4.0
2020-07-23T20:20:31.587Z
good stuff... if you could accept my answer instead of adding your own, that would be appreciated!
James Smith
CC BY-SA 4.0
2020-07-23T15:04:31.227Z
2020-07-23T15:04:31.227Z
Roi Agneta
<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>

CC BY-SA 4.0
2020-07-23T10:30:42.843Z
How about `attribute` function? More info here: https://stackoverflow.com/a/38771801/10964722
Piotr Pogorzelski
<hash>
Using a variable for a hash key
# 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)] %}
    
        {{ block.heading }}
        {{ block.description }}
        {{ text }}
      
{% 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, James!  

---

### Answer by username_0 on 2020-07-23. Score: 1

Thanks @james smith, 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)] %}
    
        {{ block.heading }}
        {{ block.description }}
        {{ text }}
      
{% endfor %}

> **Comments:**
> **username_2**: good stuff... if you could accept my answer instead of adding your own, that would be appreciated!  

---

