Conditional list-filter

I have two list filters with a preceding line of text, as follows:

Home of:
<$list filter="[all[tiddlers]search:Home<currentTiddler>sort[]]">

</$list>

Place of residence of:
<$list filter="[all[tiddlers]search:residence<currentTiddler>sort[]]">

</$list>

If the filters have no results I would like to hide the preceding text (i.e. hide “Home of:” if there is no result). I could do this in Excel with the “if” function. Can I do something similar here?

Also, I would like to arrange the layout in the tiddler into two columns with “Home of:” in the left column and “Place of residence of:” in the right column

Maybe a Grid CSS might do this, but is there something simpler?

<%if [all[tiddlers]search:Home<currentTiddler>count[]!match[0]] %>
Home of:
<$list filter="[all[tiddlers]search:Home<currentTiddler>sort[]]">

</$list>
<%endif%>
<%if [{$:/info/url/protocol}match[file:]] %>
Place of residence of:
<$list filter="[all[tiddlers]search:residence<currentTiddler>count[]!match[0]]">

</$list>
<%endif%>

try this code and check the docs

and this plugin maybe help you.

Here’s a demo of one way to do that with simple css grid.

1 Like

I had to edit a few ‘typos’ but have it working with:

<%if [all[tiddlers]search:Home<currentTiddler>sort[]] %>

Home of:
<$list filter="[all[tiddlers]search:Home<currentTiddler>sort[]]">

</$list>

<%endif%>

<%if [all[tiddlers]search:residence<currentTiddler>sort[]]] %>

Place of residence of:
<$list filter="[all[tiddlers]search:residence<currentTiddler>sort[]]">

</$list>

<%endif%>

I cannot quite see how to put filter blocks in the YAR resizer, it seems to be based on transclusion layouts for tiddlers.

I suspect all you need is:

<% if [search:Home<currentTiddler>] %>

<$list filter="[all[tiddlers]search:Home<currentTiddler>sort[]]"/>

<% endif %>

The “if” conditional syntax will process the content as long as there’s a result at all. SInce your list already bumps back out to all tiddlers, you don’t need to worry about which tiddler “fits” the if expression, or what the count is.

1 Like

Did you see my response on the formatting? See demo here.

1 Like

And so you don’t have to search all your tiddlers twice, I recommend reusing the results of the <% if %> filter in your list widget:

<% if [search:Home<currentTiddler>] +[format:titlelist[]join[ ]] %>

Home of:
<$list filter="[enlist<condition>sort[]]"/>

<% endif %>

(See this similar thread for how this works and why it’s more efficient!)

1 Like

Yes, that’s perfect for my use case.

Oh, I think this is another thing that you’ve reminded us of, elsewhere, about the power of conditional shortcut syntax, that I lost track of!

The tricky thing, I guess, is to make sure to format as titlelist with join, because as documentation says,

Within an “if” or “elseif” clause, the variable condition contains the value of the first result of evaluating the filter condition. https://tiddlywiki.com/#Conditional%20Shortcut%20Syntax

Hence the need to join, and then enlist, in order to make use of the actual list rather than first result. (Any reason not to tuck the “sort” into the outer <% if %> expression as well?)

enlist does not seem to be quite so nice as it concatenates all the results into one long string, not even space or preferably comma and space separated.

Just habit! I generally try not to sort lists (including lists stored as variables) until actually needed for display purposes. But in this case, sort won’t apply if there aren’t any results, and if there are results, you’ll want them sorted anyway. So moving it to the outer filter expression should be fine — it just needs to be inserted before the format/join step:

<% if [search:Home<currentTiddler>] +[sort[]format:titlelist[]join[ ]] %>

That’s actually nothing to do with the filter I used; it displays that way because I used a self-closing $list, as @Springer also did in her example. If you want to preserve the one-item-per-line output, you could easily switch back to the “empty” list template you used in your original example:

<$list filter="[enlist<condition>]">

</$list>

Or if you’d prefer comma-separated results, you can do that too:

<$list filter="[enlist<condition>]" join=", "/>
1 Like