Filter: current tiddler has at least one tag with prefix[x]

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?

-Sam

Try this:

[all[current]tags[]prefix[x]]

Or if you are using this in a $list widget to hide/show text and only want the text to appear once:

[all[current]tags[]prefix[x]first[]]

1 Like

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.

Thank you very much! :grin:
(you were very fast)

1 Like

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

For clarity:
has[blah] means “has a non-empty field named blah”,
while
has:field[blah] means “has a field (may be empty) named blah”

3 Likes

Good idea! I think I will do the same later.



  • 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 [x] 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 [x], 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.

I did some tests before answering, but I can’t get it to work, sorry. Yet it seems right to me

[all[current]tags[]prefix[x]first[]] This one works perfectly anyway, the inverted one doesn’t

Try this snippet for troubleshooting:

<$list filter="[all[current]tags[]prefix[@]then[so-tagged]else[not-so-tagged]]">
<<currentTiddler>>
</$list>

(Replace the @ with the prefix you want.)

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…

3 Likes

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.

2 Likes

This filter might need a tweak to exclude blank values, that is else[] might be returning an empty (zero length) item rather than no item:

[all[current]tags[]prefix[x]then[]else[yes]!is[blank]]
1 Like

Good advice :+1:t2:

Ah, glad I came close :sweat_smile:

It’s perfect, it works great!


Thanks very much to all three of you! :grin: