Variables in filters

Hi all!

Very basic question:

I have an example tiddler tagged “lion.” When I count the number of tiddlers tagged “lion” with the following, I get the correct count of one:

Number of tiddlers with this tag: <$count filter="
[all[tiddlers]tag[lion]]"/>

but I get a count of zero when I try defining a variable name:

<$set name=animal value="lion"> 
</$set>

Number of tiddlers with this tag: <$count filter="
[all[tiddlers]tag[<animal>]]"/>

could someone help me see what I’m misunderstanding about passing variables into filters? (This is coming up because of a more complicated filter where I’d like to in pass )

Thanks!

You should omit the brackets when using a variable name in a filter expression :slight_smile:

<$set name=animal value="lion"> 

Number of tiddlers with this tag: <$count filter="
[all[tiddlers]tag<animal>]"/>

</$set>

{Edit] After reading Eric’s detailed and helpful explanation I noticed that I missed the closed SetWidget, too. Therefore I corrected my code above.Sorry!
Thank you, Eric.

2 Likes

Two issues with your example code:

  • The animal variable has to be “in scope” when it is referenced. This means that the filter syntax has to occur in between the $set and /$set.
  • The brackets surrounding a filter parameter indicate how to process that parameter:
    • square brackets [something] are used for literal text values
    • angle brackets <somevar> are used for variable references
    • curly brackets {!!somefield} or {tiddlertitle} are used for tiddler field references

Note also that, when assigning a simple value to a variable, the $let widget is preferred, as it has a much more compact syntax:

<$let animal="lion">...</$let>

Thus, your code should look something like this:

<$let animal="lion">
Number of tiddlers with this tag:
<$count filter="[all[tiddlers]tag<animal>]"/>
</$let>

enjoy,
-e

3 Likes

Thank you both so much! This is very helpful!

1 Like

@Brian_Radspinner , I am a great fan of your Those Pesky brackets… page, I have it open whenever I am doing TW stuff. I would like to contribute to the content but see no easy way to do this. Certainly, I think @EricShulman comments above , wrt brackets, could almost be an opening page as whenever I have a problem, I have no simple way to map my problem on to the correct tiddler in your site. Eric’s explanation, three simple lines, could be a useful entry into your tw’s page. @EricShulman, this could also be true for TW’s documentation.

Brian, maybe you could also provide an email link for more suggestions, feedback, etc.

bobj