I would like to create a filter that takes the tags of the current tiddler with a specific prefix.
I need to differentiate the tiddlers that have at least one tag with that prefix from those who don’t. (i.e. I would like to hide and show some text in the tiddler based on the output of the filter.)
[all[current]tags[]has[prefix[x]]
I’ve tried something like this, but filters are really my Achilles tendon, how should I do?
Yes maybe I should have specified how I was going to use it, but you’ve already thought about that!
In fact, my difficulty likely arose from the use of the filter in the $list.
Sometime I’d like to write out my glossary of filter-language, where the shorter expressions are paired with what they really mean.
has[blah] means has-value-in-field[field] [edited as per tip by @EricShulman] prefix[blah] means starting-with[string]
etc. The filter expressions are short for the sake of convenience, but the fact that “prefix” is a noun in English, and “has” seems to go with “prefix” to form a coherent idea (“has[prefix]”) is misleading until we memorize the more specific roles.
Keep asking! (I occasionally struggle with building filters for patterns that seem like they ought to be straightforward. Sometimes the challenge is fun, sometimes exasperating.)
I was playing a bit with the filter @saqimtiaz provided:
And I was wondering, how can I reverse this, i.e. make the text enclosed in the $list only show if there is no tag with the prefix? I tried with “!” in front of the operators (!prefix), but it didn’t work.
Also I think (but maybe I’m wrong) that putting !prefix would not solve the problem, because it would give me the tiddler with at least one tag but with no prefix , while I’d want also the tiddler without any tags
Since you really just want the filter to return a value or not depending on the tags present and aren’t interested in the actual value returned by the filter, you could invert the filter in this manner:
[all[current]tags[]prefix[x]then[]else[yes]]
If there are tags beginning with x, then return nothing. Otherwise return the word yes.
It should yield “so-tagged” in any tiddler tagged with the prefix you’re targeting, and “not-so-tagged” elsewhere…
If you have that working, then you can delete the expression for whichever condition (so-tagged or not-so-tagged) you want to skip over (in the current context), and replace <<currentTiddler>> with the stuff you do want to display (exactly when the condition is as specified, positive or negative).
Often I find that there’s some niggling little typo, like a bracket in the wrong direction, cluttering the filter expression that “should” work…
I think you answered your own question here! [all[current]tags[]!prefix[x]first[]] will give you the first tag that doesn’t start with x, or it will give you a blank result if there aren’t any such tags. But it doesn’t tell you anything about the presence or absence of tags that do start with x. So if you had a tiddler with tags “apple” and “xylophone”, the list would return “apple”, and its contents would still show up—even though you also have a tag that starts with x.
With Saq’s approach, a “xylophone” tag will give you an empty filter result (...then[]) and the list contents won’t get displayed. If there are no x-prefixed tags, the list results in “yes”, and its contents get displayed.