Search Operator

Hello again!

I’m trying to show a button only if a field named <BasisListe> does not contain the word <Items>.

My idea is something like

<%if [!search:<BasisListe>:words<Items>] %>
   show the button
<%endif %>

I think the main problem is, that it doesn’t look in the Field named with the value of <BasisListe>.

Another approach was to start with

<%if [<currentTiddler>get<BasisListe>...] %>
   show the button
<%endif %>

but I still failed.

Thanks for your help,
Ben

I’m not sure what level of background understanding you have, but it looks like you’re using angle-brackets <like this> when you want to reference regular strings.

(Unless you’ve defined variables named BasisListe and Items and you’re zooming into the <% if … %> conditional syntax within the scope of those variables…)

If you’re actually looking for the word “Items” in a field named BasisListe then you want something like:

<%if [{!!BasisListe}split[ ]match[Items]] %>

… but you say you want the button only if that word does not appear in such a field, so one intuitive way is:

<%if [{!!BasisListe}split[ ]match[Items]] %>
<% else %>
button here
<% endif %>

Ah this makes me think what you want is something more like:


<$let Items={{!!items}}>

<% if [<currentTiddler>has{!!BasisListe}get{!!BasisListe}split[ ]match<Items>] %>

<<condition>>

<% else %>

@@ button here @@

<%endif %>

</$let>

That should retrieve a fieldname from the field called BasisListe (and the {!!curlybracketfieldname} shows that we’re reaching THROUGH that field, transcluding its value)… and look for the <Items> (however that variable has been defined — I just made it look at field {{!!items}} here) … within that field. Show the button only if nothing results from this field check.

The variable <<condition>> offers a really useful troubleshooting step. Delete it once you don’t need it, but in the meantime, it shows what the <% if %> widget’s filter has come up with.

[Edit to add: Working demo if you want to tinker with this approach. ]

1 Like

split[ ]match<Items> is a good option if you want to find only exact word matches (and if <<Items>> does not itself contain spaces, of course!) But if you want to preserve the “fuzziness” of search (e.g. case-insensitivity or word-internal matches), you can do that too. Here’s an adaptation of @Springer’s conditional code:

<$let
	Items={{!!items}}
>
<% if [{!!BasisListe}!search:title<Items>] %>
	@@ button here @@
<% endif %>
</$let>

This assumes that BasisListe is the literal name of the field, rather than a variable <<BasisListe>> containing a different field name. If you did in fact intend <<BasisListe>> to refer to a variable defined outside the conditional, you could replace {!!BasisListe} with <currentTiddler>get<BasisListe>.

In either case, the real trick here is the use of search:title, and the key point is that TW filters use :title (and sometimes {!!title} and <currentTiddler>) to refer to the input value, whatever that is. So we can use search:title to search inside each string supplied to the search operator — regardless of whether it corresponds to an actual tiddler title.

Here are some more simple examples illustrating this principle:

{{{ [[A tiddler that does not exist!]search:title[a]] }}} = A tiddler that does not exist!

{{{ [enlist[A B C D E]search:title[a]] }}} = A

And we can use !search:title in the same way:

{{{ [enlist[A B C D E]!search:title[a]] }}} => B C D E

This lets us incorporate the negation directly into the conditional filter, so we don’t need the <% else %> case: the button can go directly after <% if ... %>.

1 Like

You can use dynamic suffixes, but as far as I know, only with a bit of indirection:

<$let fltr=`[!search:$(BasisListe)$:word[$(Items)$]]`>

<%if [<currentTiddler>filter<fltr>] %> 
  Doesn't match!
<% else %>
  Matches!
<%endif %>

</$let>

Here we define a variable fltr which embeds your actual search criteria (including the leading !) and then we use it by passing it to the filter Operator in your actual filter.

You can test this by downloading the following and dragging the resulting file onto any wiki: Dynamic suffix.json (914 Bytes)

1 Like