New Features in TiddlyWIki 5.3.0: The Substitute Filter Operator

TiddlyWiki 5.3.0p will support sustitute filter operator which mimics the sprintf in C and format in Python! It allows replacing within each title placeholders for filters, parameters and variables with their corresponding values. This will simplify concatenation of strings and variables and can have a lot use cases.

Special tanks to @saqimtiaz for this great filter operator.

Ref:

NOTE: The PR is in review state.

5 Likes

Example 1:

<$let time="morning">
<$text text={{{ [[Something about $1$ in the $(time)$ at $2$.]substitute[Maths],[the Library]] }}}/>
</$let>

Produces

Something about Maths in the morning at the Library.

2 Likes

Example 2: Building a complex tooltip from variables

<$let nTids=  {{{ [!is[system]count[]add[1]] }}}
      ordinal={{{ [<nTids>split[]last[]] }}}
      suffix= {{{ [<ordinal>match[1]then[st]] 
                  [<ordinal>match[2]then[nd]]
                  [<ordinal>match[3]then[rd]]
                  :else[[th]] }}} >

<$button
   tooltip= {{{ [[Create the $(nTids)$$(suffix)$ tiddler]substitute[]] }}} 
   actions="..." >
Create
</$button>
 
</$let>

Produces a tooltip like

Create 1581st tiddler


NOTE: The above code is semantic and simple to follow, the below alternative is a more compact form of above example.

<$let nTids=  {{{ [!is[system]count[]add[1]] }}} pos="st nd rd"
      suffix= {{{ [<nTids>split[]last[]] :map[enlist<pos>nth<currentTiddler>else[th]] }}} >

<$button
   tooltip= {{{ [[Create the $(nTids)$$(suffix)$ tiddler]substitute[]] }}} 
   actions="..." >
Create
</$button>
 
</$let>
2 Likes

Example 3: The order of parameters is no matter

<$let time="morning">
<$text
 text={{{ [[Something in the $(time)$ at $2$  about $1$ ]substitute[Maths],[the Library]] }}}
/>
</$let>

Produces

Something in the morning at the Library about Maths

Note that the first parameter is referenced last

1 Like

Thanks for sharing @Mohammad

It makes it easier to read the string, than using +[join[ ]] and you can then read the substitute operator only if you need the details. It’s more visual.

Nice work @saqimtiaz

note @Mohammad

The order does matter but the order in which they are used in a string is determined by their number eg $2$ $1$

Thanks for the example, I thought it was a sub-title filter and wasn’t interested in it…

A post was split to a new topic: New Features in TiddlyWiki 5.3.0: Substitution for Widget Attributes