2

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 %}
<p><em>{{ legal.legalContent }}</em></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.

Am I missing something simple or is this not possible with globals?

2

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.

  • 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 Jun 6 '16 at 19:02
  • 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 Jun 6 '16 at 19:05
  • 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 Jun 6 '16 at 19:15
  • Ok, I understand the original question slightly better now and have updated my answer. – Luke Pearce Jun 6 '16 at 19:32
  • 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 Jun 6 '16 at 23:42
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 %}

<p><em>{{ attribute(legalPhrases, legalContentHandle }}</em></p>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.