AnswerBun.com

Using a variable for a hash key

Craft CMS Asked on December 2, 2021

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!

2 Answers

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)] %}
    <div>
        <h2>{{ block.heading }}</h2>
        <p>{{ block.description }}</p>
        <a href="#" class="button">{{ text }}</a>
    </div>  
{% endfor %}

Answered by Roi Agneta on December 2, 2021

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]

Answered by James Smith on December 2, 2021

Add your own answers!

Related Questions

Dates for entries are in different language

1  Asked on April 4, 2021 by paloma-martinez

         

Display entry category

1  Asked on April 3, 2021 by andy-kirkpatrick

 

How to get an input value without refreshing with twig?

2  Asked on March 30, 2021 by alexis-dalai-waldo-jimnez

     

Order by reverse relationship count

1  Asked on March 29, 2021 by oscaralexander

     

Output javascript inside twig variable

1  Asked on March 25, 2021 by triplethreat23

       

Control Panel or admin dashboard not found

2  Asked on March 23, 2021 by falcon

     

Super Table Field error while editing the content

0  Asked on March 21, 2021 by bhashkar-yadav

   

Relative Asset URL

1  Asked on March 19, 2021

 

Need Swift Mailer to send emails as UTF-8

1  Asked on March 17, 2021 by lily-k

   

Simple Product Listing Pages by Category Slug

1  Asked on March 16, 2021 by doctorgloom

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir