Why my conditional button does not work

Hi community,

I have a button working fine
<$button>
<$action-setfield $tiddler={{!!session_name}} list_sas-entree={{!!list}}/>
<$action-navigate $to={{!!session_name}}/>
Append session with this sequence
</$button>

I try to encapsulate it into a $list to enable a conditional display like that
<$list filter="[get[category_val]match[Preparation]]">
<$button>
<$action-setfield $tiddler={{!!session_name}} list_sas-entree={{!!list}}/>
<$action-navigate $to={{!!session_name}}/>
Append session test
</$button>
</$list>

The conditional display works (I see the button Append Session test) but when I click on the button nothing appends …

Is there a reason ? What is wrong in my code ?

Thanks very much for your support & help

Regards

Resolved

It seems that I forgot to consider that, within the $list, the currentTiddler is changing. I’ve found a solution with a $var fixing the variable outside the $list. This works

<$vars
sess={{!!session_name}}
lst={{!!list}}

<$list filter="[get[category_val]match[Posture]]">
<$button>
<$action-setfield $tiddler=<> list_posture=<>/>
<$action-navigate $to=<>/>
Append session test
</$button>
</$list>
<$list filter="[get[category_val]match[Preparation]]">
<$button>
<$action-setfield $tiddler=<> list_preparation=<>/>
<$action-navigate $to=<>/>
Append session test
</$button>
</$list>
</$vars>

You are using $list as a conditional: “if category_val=Preparation”. However, the $list widget also sets the currentTiddler to the result of the filter (i.e., “Preparation”). Thus, when you later refer to {{!!session_name}} you are referring to a field in a tiddler named “Preparation”.

To prevent the $list widget from changing the currentTiddler value, use variable=none like this:

<$list filter="[get[category_val]match[Preparation]]" variable=none>

Also, you should note that starting your filter with [get[category_val]... will return all tiddlers that have that field, which could result in many $button widgets being rendered. You probably only mean to check the category_val field in the current tiddler, like this:

<$list filter="[<currentTiddler>get[category_val]match[Preparation]]" variable=none>

Assuming this is your intent, you can then simplify your filter, like this:

<$list filter="[<currentTiddler>category_val[Preparation]]">

which checks only the current tiddler’s category_val field for “Preparation”, and also has the advantage of returning the <currentTiddler> title rather than the field value, so you don’t need to use the variable=none trick.

enjoy,
-e

Thanks a lot Eric !
I was not aware of this variable=none option
The category_val is coming from a select box whose list of value is well controled and defined. So no risk

It’s always a pleasure to get so accurate answer very quickly !

Thanks again for your support

Technically, variable=none is not a special option. You could just as easily write variable=_ or variable=arglebargle. I just use “none” as a throw-away variable that never gets referenced anywhere else.

I only recently discovered <%if …%> statements, and have been using those a lot lately instead, as these feel more readable to me. They take a filter statement and show their content if the filter is not empty, so this should work:

<%if [get[category_val]match[Posture]] %>
  <$button>
  …
  </$button>
<%elseif [get[category_val]match[Preparation]] %>
  <$button>
  …
  </$button>
<%endif%>

You would find details on this on tiddlywiki.com

What I suggest here is deciding on a personal standard and using that consistently in your own solutions, I use ~ to name a variable I am unlikely to use, however I can always make use of the variable within such lists with <<~>>, which outside such lists I have a small macro which returns the current tiddler title in plain text allowing me to use the current title in my wiki text.

  • Only a small subset of lists require the variable name to leave currentTiddler intact, however you can often make use of <<storyTiddler>>
  • I try and use the default variable name currentTiddler where possible and all my macros assume the currentTiddler is what it operates on. This means I dont need to pass the target tiddler title as long as the currentTiddler has the appropriate value due to a list widget, let/vars/or Set widget, or even the $tiddler widget.
    • This is copying the way tiddlywikis core often works as well.