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!
attribute
function? More info here: stackoverflow.com/a/38771801/10964722