findTaxonomy with variables

I know I probably reached my limit today for asking questions, but from my TWIG I can use the findTaxonomy function quite nicely but only when it is hardcoded. I am trying to make the TWIG flexible in that I can set YAML header options to make it display different content

Trying to do something like

{% for feature in taxonomy.findTaxonomy({'{{ page.header.taxfeatures.type }}': '{{ page.header.taxfeatures.filter }}'}) %}

and in my item.md:

taxfeatures:
  type: dna
  filter: applications

but the findtaxonomy function does not return any items. if I hard code it to do:

{% for feature in taxonomy.findTaxonomy({'dna': 'applications'}) %}

then it will work. I verified in the same twig that {{ page.header.taxfeatures.type }} and {{ page.header.taxfeatures.filter }} are legit.

I also trie various ways of doing the hash as a variable hash but only got TWIG errors

You can’t nest tags, but nor do you need to:

{% for feature in taxonomy.findTaxonomy({page.header.taxfeatures.type: page.header.taxfeatures.filter}) %}

I tried what you suggested

{% for feature in taxonomy.findTaxonomy({page.header.taxfeatures.type: page.header.taxfeatures.filter}) %}

but am getting a TWIG syntax error:

A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "." ("punctuation" expected with value ":") 

I also tried the following

{% for feature in taxonomy.findTaxonomy({ '{{page.header.taxfeatures.type}}' : '{{page.header.taxfeatures.filter}}' }) %}

in this case no error, but nothing is found

FWIW, I was able to get this to work:

{% set tax_type = page.header.taxfeatures.type %}
{% set tax_filter = page.header.taxfeatures.filter %}
    {% for feature in taxonomy.findTaxonomy({'dna' : tax_filter}) %}

so it seems like it does not like the hask ‘key’ to be a variable.

@Robert Yes, Twig does not like a hash key to be a variable. But you can protect the variable, meaning it will be evaluated first, then set as a hash by using parentheses around:

{% for feature in taxonomy.findTaxonomy({(page.header.taxfeatures.type): page.header.taxfeatures.filter}) %}
---
1 Like

@sommerregen That did the trick. Awesome! - would I find this in the TWIG documentation ? I could not see where this would be.

@Robert It is an undocumented feature. I found it, once I have had the same problem like you on StackOverflow. Don’t know the URL anymore…

Oh yah @sommerregen is absolutely right. I didn’t test my code! But parens are a simple way to restrict dot notation to a specific variable. Otherwise you need to use attribute() function: http://twig.sensiolabs.org/doc/functions/attribute.html