Substituted Attribute Values and Dynamic Filters

Substituted Attribute Values is a new feature introduced in TiddlyWiki 5.3.0 which has not gotten enough attention. It is quite powerful. I have partially discussed this feature here: New Features in TiddlyWiki 5.3.0: Substitution for Widget Attributes - Discussion - Talk TW

Dynamic filters
One use case is creating dynamic filter. Look at the below example

\procedure mySearch(field:"title", flag:"caseinsensitive", term:"", len:"1")
<$list filter=`[!is[system]search:$(field)$:$(flag)$[$(term)$]] :filter[length[]compare:number:gt[$(len)$]]`
       template="$:/core/ui/ListItemTemplate"/>
\end mySearch

Here the procedure mySearch has four input attributes: field, flag, term and len. The procedure uses the $list widget to display tiddlers based on a dynamic filter:

`[!is[system]search:$(field)$:$(flag)$[$(term)$]] :filter[length[]compare:number:gt[$(len)$]]`

Note to the backticks. The value of the attribute will be the text denoted by the backticks with any of the placeholders for filter expressions and variables substituted with their corresponding values. Filter expression placeholders are substituted before variable placeholders, allowing for further variable substitution in their returned value. In the above example only variable placeholder is used.

This construct allows to create dynamic filter on the fly. :star_struck:
If mySearch called like below

<<mySearch term:Font len:18>>

the filter will be generated as:

[!is[system]search:title:caseinsensitive[Font]] :filter[length[]compare:number:gt[18]]

If mySearch called like below

<<mySearch field:"tags" flag:"literal,casesensitive" term:"Filter Operator">>

the filter is created as:

[!is[system]search:tags:literal,casesensitive[Filter Operator]] :filter[length[]compare:number:gt[20]]

Note how the place holders are substituted, and the final filter is created.

Give a try on TiddlyWiki 5.3.3

TiddlyWiki 5.3.x is Great!

References
[1]. https://tiddlywiki.com/#Substituted%20Attribute%20Values
[2]. https://tiddlywiki.com/#Release%205.3.0
[3]. Substituted Attribute Values - Discussion - Talk TW (tiddlywiki.org)

7 Likes

This post made me realize why I didn’t get this to work previously; the backticks. (Yes, it does say so in the docs but…)

No worky:

<$list filter="""[!is[system]search:$(field)$:$(flag)$[$(term)$]] :filter[length[]compare:number:gt[$(len)$]]"""
       template="$:/core/ui/ListItemTemplate"/>

- nor -

<$list filter='[!is[system]search:$(field)$:$(flag)$[$(term)$]] :filter[length[]compare:number:gt[$(len)$]]'
       template="$:/core/ui/ListItemTemplate"/>

…i.e as Mohammad points out, and as is brought up in the docs, the filter needs to be encapsulated using backticks!