Syntax question about variable expansion in the list widget

Hi,

Should this work?

\define is_search_disabled()
[all[current]{!!search_tmp}length[]compare:number:lt[2]then[yes]else[no]]
\end

<$button disabled=<<is_search_disabled>>>
 Search
</$button>

The idea is to disable the button if the length of the search_tmp field is less than 2. It seems that even if the content of the macro is simply yes, it doesn’t work.

Also, if there’s a way to simplify that expression, please let me know, still learning :slight_smile:

In order to calculate the results of the filter syntax, you need to use “filtered transclusion”, like this:

<$button disabled={{{ [subfilter<is_search_disabled>] }}}>
  • The outer {{{ and }}} cause the filter syntax to be evaluated
  • The subfilter<...> syntax invokes a filter stored in a variable (note: macro definitions are technically variables)

To simplify the filter expression, use:

\define is_search_disabled() {!!search_tmp}length[]compare:number:lt[2]then[yes]
  • Omit all[current]
    Since the {!!search_tmp} syntax that follows it doesn’t specify a tiddler title, it uses the field value from the currentTiddler by default.

  • Omit else[no]
    The disabled={{{ [subfilter<is_search_disabled>] }}} parameter will only disable the button if the filter result is “yes”. Thus, returning the length of the search text is equivalent to returning “no”.

1 Like

Awesome!

Ahh, interesting.

Why would this not work though?

\define is_search_disabled() {{{ [{!!search_tmp}length[]compare:number:lt[2]then[yes]] }}}

<$button disabled=<<is_search_disabled>>>...

Also is that the only purpose of subfilter to evaluate a filter from a variable? I read the description of that operator many times and never quite understood its purpose.

When a macro is used as a widget parameter value, its content is simply used as the literal value of the parameter, without being “wikified”. Thus, your code above would be equivalent to: (note the double quotes surrounding the value)

<$button disabled="{{{ [{!!search_tmp}length[]compare:number:lt[2]then[yes]] }}}">...

In contrast, using the {{{ and }}} around the widget parameter value DOES cause the filter syntax to be evaluated, so that the parameter value is the desired filter results, rather than the filter syntax itself.

Also is that the only purpose of subfilter to evaluate a filter from a variable? I read the description of that operator many times and never quite understood its purpose.

The subfilter filter operator can also be used to evaluate a filter that is stored as the contents of a tiddler field or index, as in:

subfilter{!!somefield}
or
subfilter{##someindex}
or
subfilter{sometiddler}
(which is equivalent to subfilter{sometiddler!!text})
1 Like

Thanks Eric for helping me understand this better. I did go through GrokTW book and the chapter on wikification and what you said jives with what I read there:

when you use a transclusion (whether of a field or a macro/variable) as a parameter to an HTML or widget attribute, the result of this transclusion is not wikified . In other words, TiddlyWiki doesn’t attempt to repeatedly wikify the result of the initial transclusion like it does with body text; it does it exactly once and then stops.

I forgot about that!

I tried to inspect the HTML code for the disabled attribute and nothing was there at all for the disabled part. Is it possible to see the generated value of that somewhere?

Hi if you try the following code with https://tiddlywiki.com/prerelease v5.3.0-pre it works as expected.

See the \function pragma instead of the macro definition. That’s one of the improvements made with v5.3.0

\function is_search_disabled() [{!!search_tmp}length[]compare:number:lt[2]then[yes]else[no]]

<$button disabled=<<is_search_disabled>>>
 Search
</$button>
1 Like

That’s pretty cool. Thanks for making me aware of the new version coming out with its many improvements. Just looked fore the first time!