How can I make this filter work? (And how can I make it more efficient?)

Hi, I’m new here and still learning the mechanics of TiddlyWiki, I have to say it’s a great piece of software.

I’m getting the hang of it, but I’m finding the filters less easy to use than I’d hoped. But I’m studying them and I hope that soon I can “master” them too.

I have a question about a mechanism that uses filters that I’m building:

<$list filter="[all[current]get[example]!is[blank]]">
{{!!example}}
</$list>
<$list filter="[all[current]get[example]is[blank]]">
field is empty
</$list>

This should cause the exaple field to be transcluded if the exaple field is not empty, and cause a customizable message to appear if it is empty instead.


First of all, what I’ve shared now doesn’t work, but I can’t figure out why. But I also belive there may be a more efficient solution using conditional operators https://tiddlywiki.com/#Conditional%20Operators

Could anyone give me some pointers?

ok, so, sorry, not technically an answer to your question, but you can do it this way instead…

<$reveal type=“nomatch” state="!!example" text="">
{{!!example}}
</$reveal>
<$reveal type=“match” state="!!example" text="">
error no example! {{defaultExample!!example}}
</$reveal>

The important thing to understand about list filters is that, within the widget contents, <<currentTiddler>> refers to the item output by the list, not to the tiddler that contains the widget.

  • In this case, get[example] returns the contents of the current title’s “example” field, if it exists. This becomes the new <<currentTiddler>> / {{!!title}} within the context of the list.
  • That means that any field transclusions you use within the widget contents, like {{!!example}}, will be looking for a field called “example” on the tiddler called “Whatever you put in the example field on the original tiddler”.

Here are a couple of functional alternatives that accomplish the same thing:

<$list filter="[all[current]has[example]]">
{{!!example}} <!-- Note that unlike get, has does not change the title value; it simply returns the input title IF that tiddler has an "example" field with anything in it. -->
</$list>

<$list filter="[all[current]get[example]]">
{{!!title}} <!-- "title" is a bit of a misnomer: it refers to the current value of the list, not necessarily an extant tiddler. Also note that you don't need !is[blank], because the list will automatically return a null result if there's nothing to "get". -->
</$list>

<$list filter="[all[current]get[example]]" variable="has-example">
{{!!example}} <!-- If we explicitly set a variable to represent the value of the list output, <<currentTiddler>> won't be overwritten, and its original value will be preserved. -->
</$list>

But, as you said, there’s a simpler way to achieve this specific result! Here’s what I’d do:

<$list filter="[all[current]has[example]]" emptyMessage="field is empty">
{{!!example}}
</$list>

emptyMessage lets us specify a fallback value (which could be as simple or complicated as you want, even including other lists if necessary.)

And just for fun, here’s one more possibility using a conditional filter run prefix, ~ / :else:

<$list filter="[all[current]get[example]] ~[[field is empty]]">
{{!!title}} <!-- Remember that "title" just means "current output value" -->
</$list>

The :else prefix can be used before whole filter runs, not just literal text/titles, so you could do something like this…

<$list filter="[all[current]get[example]] ~[all[current]get[caption]]">

… to display e.g. the tiddler’s caption (assuming it has one) if there’s nothing in the “example” field.

2 Likes

Thanks to both, @AquilaRoss I tried to test what you proposed but I couldn’t get it to work, but thank you all the same!

@etardiff Wow, that was really nice of you. The explanation you gave me is really detailed and comprehensive, thank you very much!

I will treasure it, it seems to me something that will happen often

It works perfectly! :grinning:

1 Like

Welcome @SteeringWheels just to extend your understanding of the list widget, is you can change the name of the variable inside the list widget using the variable=variablename parameter. That is rather than having the currentTiddler variable change.

However it is useful to try and write your solutions to act on the current tiddler because the code is more reusable. It can be copied and reused, placed in a macro etc…

  • Its good to develop habits guided by others expierence.
1 Like