Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

findTaxonomy with variables

Started by Muut Archive 11 years ago · 7 replies · 616 views
11 years ago

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

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

and in my item.md:

YAML
taxfeatures:
  type: dna
  filter: applications

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

TWIG
{% 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

👍 1
11 years ago

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

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

I tried what you suggested

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

but am getting a TWIG syntax error:

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

I also tried the following

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

in this case no error, but nothing is found

11 years ago

FWIW, I was able to get this to work:

TWIG
{% 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.

11 years ago

@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:

TWIG

{% for feature in taxonomy.findTaxonomy({(page.header.taxfeatures.type): page.header.taxfeatures.filter}) %}
---
👍 1
11 years ago

@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...

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1345 9 years ago
Archive · by Muut Archive, 9 years ago
2 932 9 years ago
Archive · by Muut Archive, 9 years ago
2 4059 9 years ago
Archive · by Muut Archive, 9 years ago
1 2945 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago