<p><strong>UPDATE:</strong> Knowing your use case I would use a dropdown as you were inside your matrix block and then query what the chosen value was and output the relevant global field.</p>

<pre><code>{% if block.dropdownValue == 'globalName' %}
   {{ globalHandle.globalField }}
{% endif %}
</code></pre>

<p>You won't be able to do this dynamically but as you're adding the values to the dropdown matrix field you can add each 'case' to your template?</p>

<hr>

<p>What you're trying to do isn't the usually use case for Globals. Globals are used more to output values in your templates independently from the current context (entry or section). </p>

<p>I would probably add the languages as categories and use a category field within your Matrix. This won't provide a dropdown but the UI for choosing a category has some extra features like the ability to search.</p>

<p>You will have the same control over the content as you do using Globals by adding fields to Categories.</p>

CC BY-SA 3.0
2016-06-06T19:02:16.423Z
Coming over from ExpressionEngine That seems like a _really_ odd restriction - there are plenty of use cases for having small global chunks of content you want want to show differently based on a page or entry. I guess I will try categories or maybe just related entries. Thanks!
FortySeven Media
CC BY-SA 3.0
2016-06-06T19:05:53.257Z
You are going to love Craft coming from EE :) If you could give some details of your use case in your original question, I might be able to offer a more appropriate solution.
Luke Pearce
CC BY-SA 3.0
2016-06-06T19:15:00.643Z
Essentially, I am adding some boilerplate content for blog posts. One says something about the links being affiliate links and the other says the product we're reviewing was given to us. I want to be able to re-use these two snippets and inject them into the content wherever I want on each post. I'm using a Matrix content block area and trying to add it in that way.
FortySeven Media
CC BY-SA 3.0
2016-06-06T19:32:47.423Z
Ok, I understand the original question slightly better now and have updated my answer.
Luke Pearce
CC BY-SA 3.0
2016-06-06T23:42:53.173Z
Gotcha, yeah that makes sense. It would be nice if it was more dynamic, but this shouldn't be changing very often. Thanks :)
FortySeven Media
CC BY-SA 3.0
2016-06-06T18:42:19.283Z
2016-06-06T19:31:55.670Z
2016-06-06T19:31:55.670Z
Luke Pearce
<p>If I'm understanding the question properly, you have a <code>dropdown</code> field in a <code>matrix</code> block that lets the user pick out which phrase they want to spit out on the page. The actual <em>content</em> of that phrase is defined in a global (with the same field handle name).</p>

<p>Craft treats fields as content, which means the phrases you stored within those 2 fields is translatable, etc. Behind the scenes, when you do a <code>legal.fieldName</code> call to a global, it's doing a bunch of things grab the field's actual content. </p>

<p>It's also not like a one-to-one map like you might call a variable inside PHP.</p>

<p>Twig's <a href="http://twig.sensiolabs.org/doc/templates.html#literals" rel="nofollow"><code>attribute</code></a> function could get you close here if you setup a hash with the values from the fields first.</p>

<pre><code>{# two global variables, legal.dumbLawyerLegalese and legal.moreDumbLawyerLegalese #}

{% set legalPhrases = {

      'dumbLawyerLegalese': legal.dumbLawyerLegalese,
      'moreDumbLawyerLegalese': legal.moreDumbLawyerLegalese 
      }

%}

{# get the value of the dropdown #}
{% set legalContentHandle = block.legalVariables.value %}

&lt;p&gt;&lt;em&gt;{{ attribute(legalPhrases, legalContentHandle }}&lt;/em&gt;&lt;/p&gt;
</code></pre>

<p>Attribute in Twig would be like <code>$legalPhrases[$legalContentHandle]</code> in PHP. </p>

CC BY-SA 3.0
2016-06-07T00:36:29.163Z
2016-06-07T00:36:29.163Z
RitterKnight
<p>I have two globals setup for affiliate and compensated type language - using Matrix and a dropdown I'm trying to find a way to let the user add this language in a new block.</p>

<p>I cannot for the life of me get the globals to parse. In my dropdown handle I've made sure to mirror the handle of the globals themselves. Then I'm using something like this:</p>

<pre><code>{% case 'globals' %}
{% set legalContent = block.legalVariables %}
&lt;p&gt;&lt;em&gt;{{ legal.legalContent }}&lt;/em&gt;&lt;/p&gt;
</code></pre>

<p>'globals' is the block handle, 'legalVariables' is the dropdown handle and 'legal' is the global group. I've tried quite a few different variations on this, but it just spits out 'legal.affiliateLegal' which is right, but it won't parse it.</p>

<p>Am I missing something simple or is this not possible with globals?</p>

<matrix><globals><drop-down>
Using a Dropdown field in a Matrix to parse a global
# Using a Dropdown field in a Matrix to parse a global

(Asked by: username_0 on 2016-06-06)

I have two globals setup for affiliate and compensated type language - using Matrix and a dropdown I'm trying to find a way to let the user add this language in a new block.
I cannot for the life of me get the globals to parse. In my dropdown handle I've made sure to mirror the handle of the globals themselves. Then I'm using something like this:
{% case 'globals' %}
{% set legalContent = block.legalVariables %}
{{ legal.legalContent }}

'globals' is the block handle, 'legalVariables' is the dropdown handle and 'legal' is the global group. I've tried quite a few different variations on this, but it just spits out 'legal.affiliateLegal' which is right, but it won't parse it.
Am I missing something simple or is this not possible with globals?

**Tags:** matrix, globals, drop-down

---

## 2 answers

### Answer by username_1 on 2016-06-06. Score: 2 | Accepted answer

UPDATE: Knowing your use case I would use a dropdown as you were inside your matrix block and then query what the chosen value was and output the relevant global field.
{% if block.dropdownValue == 'globalName' %}
   {{ globalHandle.globalField }}
{% endif %}

You won't be able to do this dynamically but as you're adding the values to the dropdown matrix field you can add each 'case' to your template?

What you're trying to do isn't the usually use case for Globals. Globals are used more to output values in your templates independently from the current context (entry or section). 
I would probably add the languages as categories and use a category field within your Matrix. This won't provide a dropdown but the UI for choosing a category has some extra features like the ability to search.
You will have the same control over the content as you do using Globals by adding fields to Categories.

> **Comments:**
> **username_0**: Coming over from ExpressionEngine That seems like a _really_ odd restriction - there are plenty of use cases for having small global chunks of content you want want to show differently based on a page or entry. I guess I will try categories or maybe just related entries. Thanks!  
> **username_1**: You are going to love Craft coming from EE :) If you could give some details of your use case in your original question, I might be able to offer a more appropriate solution.  
> **username_0**: Essentially, I am adding some boilerplate content for blog posts. One says something about the links being affiliate links and the other says the product we're reviewing was given to us. I want to be able to re-use these two snippets and inject them into the content wherever I want on each post. I'm using a Matrix content block area and trying to add it in that way.  
> **username_1**: Ok, I understand the original question slightly better now and have updated my answer.  
> **username_0**: Gotcha, yeah that makes sense. It would be nice if it was more dynamic, but this shouldn't be changing very often. Thanks :)  

---

### Answer by username_2 on 2016-06-07. Score: 1

If I'm understanding the question properly, you have a dropdown field in a matrix block that lets the user pick out which phrase they want to spit out on the page. The actual content of that phrase is defined in a global (with the same field handle name).
Craft treats fields as content, which means the phrases you stored within those 2 fields is translatable, etc. Behind the scenes, when you do a legal.fieldName call to a global, it's doing a bunch of things grab the field's actual content. 
It's also not like a one-to-one map like you might call a variable inside PHP.
Twig's attribute function could get you close here if you setup a hash with the values from the fields first.
{# two global variables, legal.dumbLawyerLegalese and legal.moreDumbLawyerLegalese #}

{% set legalPhrases = {

      'dumbLawyerLegalese': legal.dumbLawyerLegalese,
      'moreDumbLawyerLegalese': legal.moreDumbLawyerLegalese 
      }

%}

{# get the value of the dropdown #}
{% set legalContentHandle = block.legalVariables.value %}

{{ attribute(legalPhrases, legalContentHandle }}

Attribute in Twig would be like $legalPhrases[$legalContentHandle] in PHP.


---

