TiddlyWiki loop construct for generating tiddler content

Is there a way to express a loop to repeat a block of content with some minor change each cycle. For example, what would be a goog way to loop over a set of tags to generate a page containing a series of html tables, each displaying information about tiddlers tagged with one of the tags in the set?.. .

The simple answer is yes. With filters and the list widget, possibly nested and possibly using templates and transclusions.

Your question is hard to address in full without making a lot of assumptions and taking some time. Perhaps you can break it down into some simpler questions.

Thanks you. Essentially I was naively looking for the tiddlywiki equivalent of this bash construct:

for x in 1 2 3 4; do
echo $x
done

The TW use case which inspired my question:

I have a bunch of foobar tiddlers, each of which is tagged with one of “A”, “B”, “C”, etc, I want to create a ‘foobar report tiddler’ which has an html table for each of these tags, containing information about the tiddlers with that tag. The first table has rows for tiddlers tagged with “A”, The second table has rows for tiddlers tagged with “B”, etc.

At the moment I have a macro which take a ‘tag’ argument and creates a table which filters on that tag. I then ‘manually’ call it repeatedly, once for each tag. The macro saves a lot of typing but I am looking for a way to call this macro in a loop over that tags of interest.

I don’t know bash so I might misunderstand it but the following might be an implementation (rather than specifically 4 items, it checks for all items that are tagged with x)

<$list filter='[tag[x]]'>
<$link/>
</$list>

Anyway, as Tones notes it is difficult to undertand what exactly you mean, but my general impression is that TW can do anything you ask for in your post. You should play around with it, especially the ListWidget is powerful.

1 Like

THank you.
Is https://tiddlywiki.com/#ListWidget the best place to get comprehensive explanation of the ListWidget?

I think the answer to my question may be in the examples on the GroupedLists page of tiddlywiki.org, I am trying to decipher it.

Yes, that is the place for the official documentation and that is the specific doc for this widget.

Regarding tiddlywiki.org - I’m frankly not sure what it is used for these days. I suggest you disregard that site.

So the key knowledge to gain about tiddlywiki is its filter syntax, it takes a little while to understand but is very powerful, as powerful or more so than say SQL Structure Query Language.

  • Filters treat tiddler titles as the key input (although any set of strings can be used).
  • When repeating something such as your for loop, it “terminates” automatically after all items are processed.
<$list filter="1 2 3 4">

</$list>

But lets look at your mode detailed requirment

<$list filter="A B C +[is[tag]]" variable=tag-tiddler>
<<tag-tiddler>> Header
   <$list filter="[tag<tag-tiddler>]">
     
   </$list>
Footer<br>
</$list>
  • The +[is[tag]] test each to confirm it is used as a tag, if not don’t list
  • The header and footer occur for each tag
  • The inner list, lists the items with that tag
    • A list widget with an empty line effectively defaults to <$link/><br>
  • There are different ways to turn this into a table,

But as you are a very new user perhaps a plugin such as dynamic tables would add a lot more value. You may still use the outer list.

Given your pseudocode example

  • we tiddlywikians would simplify this to

filter=“1 2 3 4”

1 Like

Very helpful. Thank you.

I was looking for a generic mechanism for looping over a list of arbitrary strings and your answer showed that. I mentioned my use case involved tags so your +[is[tag]] showed a succinct way to add contraints with plus sign.

For others new to TW, here is the experiment I ran to show nested loops and the way to invoke a macro that takes the loop vars as input. I was not able to get the macro to work with simple <<pretty x y z >> but is does work as shown with macrocall widget.

This tiddler text…

\define pretty(a, b, c) 
  $a$ - $b$ - $c$ <br>
\end
 
<$list filter="A B C" variable=x>

!!<<x>>
   <$list filter="1 2 3" variable=y>

 !!!<<x>>-<<y>>
     <$list filter="p q r" variable=z>
         <$macrocall $name="pretty" a=<<x>>  b=<<y>> c=<<z>>/>
     </$list>
   </$list>
</$list>

renders thusly …

A

A-1

A - 1 - p
A - 1 - q
A - 1 - r

A-2

A - 2 - p
A - 2 - q
A - 2 - r

A-3

A - 3 - p
A - 3 - q
A - 3 - r

B

B-1

B - 1 - p
B - 1 - q
B - 1 - r

B-2

B - 2 - p
B - 2 - q
B - 2 - r

B-3

B - 3 - p
B - 3 - q
B - 3 - r

C

C-1

C - 1 - p
C - 1 - q
C - 1 - r

C-2

C - 2 - p
C - 2 - q
C - 2 - r

C-3

C - 3 - p
C - 3 - q
C - 3 - r

2 Likes

I don’t have time to respond in depth now, but … it should never be necessary to create a bunch of tiddlers just to represent the numbers for which you’re repeating a process.

Usually the range operator will suffice for getting you any set of numbers you could wish for:

https://tiddlywiki.com/#range%20Operator

If you think you need to generate a set of tiddlers, say why?

2 Likes