Answers.Body: <p>Unless I'm misunderstanding, the title of your question is a bit misleading as you're not trying to call a Craft function in Twig, you're trying to output a dynamically named variable (a variable variable).</p>
  
  <p>You can generally do that using the array syntax: e.g. <code>categories[catOption]</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-25T14:16:22.480Z
Answers.Text: You are the real MVP! And yes, I was wondering how to format my title. A variable variable was indeed the closest I could call it. Thank you!
Answers.UserDisplayName: ves
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-25T11:49:12.023Z
Answers.LastActivityDate: 2019-09-25T11:49:12.023Z
Answers.UserDisplayName: James Smith
Body: <p>I am using Craft 3 with the Commerce 2 plugin.</p>
  
  <p>I am trying to dynamically call a variable in a twig template where this variable has to be a "placeholder" for a handle of a field inside a block inside a matrix field.</p>
  
  <p>Here is an example snippet which visualises my problem:</p>
  
  <pre><code>{# 'product' variable is passed to this template #}
  
  {% set fields = product.productFields %}
  {% set categoryOptions = [] %}
  {% for cats in fields.type('categories') %}
      {% for category in categories.fieldLayout.fields %}
          {% set categoryOptions = categoryOptions|merge([category.name]) %}
      {% endfor %}
  {% endfor%}
  
  {% set productId = product.id %}
  {% for catOption in categoryOptions %}
      {# Here is where I try to call catOption as a 'placeholder' #}
      {% set productId = productId~ '-'~ categories.(#{catOption}).label %}
  {# I have also tried using:
  {catOption},
  catOption,
  #{catOption} etc but nothing seems to work #}
  {% endfor %}
  </code></pre>
  
  <p>Usually I could call the 'categories' one by one but I want everything to be dynamically set.</p>
  
  <p>I know that in some languages you can call a function/property like that or assign a variable with such value. </p>
  
  <p>Any suggestion for a solution or a workaround would be useful. Thank you in advance!</p>
  
Tags: <templating><craft3><plugin-craftcommerce2>
Title: Can I call a dynamically named variable through twig?
ThreadText: # Can I call a dynamically named variable through twig?
  
  (Asked by: username_0 on 2019-09-25)
  
  I am using Craft 3 with the Commerce 2 plugin.
  
  I am trying to dynamically call a variable in a twig template where this variable has to be a "placeholder" for a handle of a field inside a block inside a matrix field.
  
  Here is an example snippet which visualises my problem:
  
  ```
  {# 'product' variable is passed to this template #}
  
  {% set fields = product.productFields %}
  {% set categoryOptions = [] %}
  {% for cats in fields.type('categories') %}
      {% for category in categories.fieldLayout.fields %}
          {% set categoryOptions = categoryOptions|merge([category.name]) %}
      {% endfor %}
  {% endfor%}
  
  {% set productId = product.id %}
  {% for catOption in categoryOptions %}
      {# Here is where I try to call catOption as a 'placeholder' #}
      {% set productId = productId~ '-'~ categories.(#{catOption}).label %}
  {# I have also tried using:
  {catOption},
  catOption,
  #{catOption} etc but nothing seems to work #}
  {% endfor %}
  
  ```
  
  Usually I could call the 'categories' one by one but I want everything to be dynamically set.
  
  I know that in some languages you can call a function/property like that or assign a variable with such value.
  
  Any suggestion for a solution or a workaround would be useful. Thank you in advance!
  
  **Tags:** templating, craft3, plugin-craftcommerce2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-09-25. Score: 2 | Accepted answer
  
  Unless I'm misunderstanding, the title of your question is a bit misleading as you're not trying to call a Craft function in Twig, you're trying to output a dynamically named variable (a variable variable).
  
  You can generally do that using the array syntax: e.g. `categories[catOption]`.
  
  > **Comments:**
  > **username_0**: You are the real MVP! And yes, I was wondering how to format my title. A variable variable was indeed the closest I could call it. Thank you!  
  
  ---
  
  