[TW5] Getting the content of several fields in a row

162 views
Skip to first unread message

Martin Pusch

unread,
Feb 12, 2020, 12:05:52 PM2/12/20
to TiddlyWikiDev
Hello, I have searched the discussions for a hint, but I still haven't managed to resolve my problem:

Example: I have a tiddler, with the title: "John 3". The text is empty, but there are fields:
Field name / Field content: 1 / There was a man of the Pharisees …
Field name / Field content: 2 / The same came to Jesus by night …
Field name / Field content: 3 / Jesus answered and said unto him …
Field name / Field content: 4 / Nicodemus saith unto him, How can …
Field name / Field content: 5 / Jesus answered, Verily, verily, I say unto thee …

Now, I can use this structure to transclude verses out of the fields in another tiddler, typing:

{{John 3!!1}} – and this statement will be replaced by the string "There was a man of the Pharisees …"

Like this, I don't need to rewrite text that is allready written.

My problem: How can I get several fields in a row? I want to be able to write something like:

{{John 3!!1-5}} – and get the content of the fields 1 … 5.

I think I need to write a macro to get what I want. But how can I address a series of fields? Is there something like a "FOR I = 1 to 5" like in Basic?

Can anybody give me a hint please?


Joshua Fontany

unread,
Feb 12, 2020, 5:28:55 PM2/12/20
to TiddlyWikiDev
Hi Martin,

Welcome to Tiddlywiki!

In order to explain this, we should examine the {{tiddlerName!!field}} syntax. This "wiki syntax" is actually shorthand for two Widgets. Widgets in tiddlywiki are HTML-like entities that you can use to render content, change tiddler/field value, etc. The shorthand is expanded to the equivalent of:
<$tiddler tiddler="tiddlerName">
<$transclude field="field" />
</$tiddler>

The tool you want to use for this is the "List Widget". To use the List Widget, you also need to understand "Filters".

https://tiddlywiki.com/#Widgets



We wrap the List Widget in a Tiddler Widget to set the "currentTiddler" context variable explicitly. Then, by passing the List Widget a value for the "variable" attribute we tell it not to overwrite the "currentTiddler" variable. There are a few ways to construct the Filter for the List widget. We can use the Range Operator to generate a range of numbers, or we can use the Fields Operator to get all fields from the "currentTiddler". If the tiddler only has numbered fields with source-text then that would be easier to sort.

Then, as we cannot pass the variable syntax ( <<name>> ) through the transclude shorthand syntax (i.e. "{{name!!<<field>>}}" DOES NOT WORK), we need to call either the View or Transclude widget directly.

<$tiddler tiddler="John 3">
<$list variable="line" filter="[
<currentTiddler>fields[]sort[]]">
<$transclude
field=<<line>> />
<$/list>
</$tiddler>

I hope that helps!

Best,
Joshua Fontany

Mat

unread,
Feb 13, 2020, 12:45:53 AM2/13/20
to TiddlyWikiDev
Joshuas answer is good, even if it's "teaching a man to fish". Just to clarify one thing he wrote:

In order to explain this, we should examine the {{tiddlerName!!field}} syntax. This "wiki syntax" is actually shorthand for two Widgets. Widgets in tiddlywiki are HTML-like entities that you can use to render content, change tiddler/field value, etc. The shorthand is expanded to the equivalent of:
<$tiddler tiddler="tiddlerName">
<$transclude field="field" />
</$tiddler>

"shorthand syntax" for transclusion refers to the {{...}} syntax. Analogously the shorthand for macro calls is "<<...>>".

The expanded syntax for translcusion could also be written:

<$transclude tiddler="my tiddler" field=field/>

i.e instead of setting the "environment" with $tiddler, you can specify which tiddler inside the $transclude widget.


<:-)


Martin Pusch

unread,
Feb 13, 2020, 5:32:34 AM2/13/20
to TiddlyWikiDev
Thanks, Joshua and Mat!

I tried to modify your example code to get a better understanding. This is my result so far:

<$tiddler tiddler="John 3">
<$list variable="line" filter="[
<currentTiddler>fields[]sort[]]">
<p><code><<line>></code> <$transclude field=<<line>> /></p>
</$list>
</$tiddler>

I'm struggling with two problems:

  1. How to narrow down the result? At the moment, I get all fields as a result, including "creator", "created" and such. I want to get just certain fields, for example 3 … 5, or 9 … 11. I tried to filter the result, but I didn't manage to do it.
  2. How to sort the resulting fields as numbers? At the moment, they are sorted alphabetically. I get an order like this: 1, 10, 11 … 19, 2 and so on. But I want an order like 1, 2, 3 … . For this, the field names have to be seen as numbers, not as text strings. How to achieve this?
I've read quite a lot of text in the last hours, and I tested all kinds of ideas. But I didn't manage to catch the fish – I need more teaching …

TonyM

unread,
Feb 13, 2020, 6:27:47 AM2/13/20
to TiddlyWikiDev
Martin,

I use a similar method, in my first and now old plugin https://tiddlywiki.psat.com.au/mymenus/ to list different menu options. You may find some methods within that. The sorting can be done with the appropriate sort operator especially if your fieldnames are logically sortable eg 1 2 3.4 10 50 11 use the nsort operator. Its also ok to have luke-1 luke-2 luke-3.4 luke-10 luke-50 luke-11 as you can either remove the prefix or since the prefix is the same it will still sort correctly.

You can do this in a filter [all[current]fields[]prefix[luke-]sort[]]
I use [all[current]fields[]prefix[menu-item-]sort[]] and [all[current]fields[]prefix[menu-item-]nsort[]] should work

Or you could have a subfilter remove items 
\define standard-fields() created modified text icon caption ....

And the filter 
[all[current]fields[]] -[subfilter<standard-fields>]

Regards
Tony

Martin Pusch

unread,
Feb 13, 2020, 6:55:00 AM2/13/20
to TiddlyWikiDev
Thanks, TonyM!

Since my last posting, I continued testing and searching. Now, I have this:

<$tiddler tiddler="John 3">
<$list variable="line" filter="[all[tiddlers]fields[]nsort[title]]+[range[3,5]]">
<p><code><<line>></code> <$transclude field=<<line>> /></p>
</$list>
</$tiddler>

Now, sorting is correct (nsort). And I managed to get just the range of fields I want as a result (range[3,5]).

The next step will be to put all this in a macro, to make it work everywhere.




Martin Pusch

unread,
Feb 15, 2020, 10:04:31 AM2/15/20
to TiddlyWikiDev
I have now my macro, and it is working so far:

\define biv(bbook,bchapter,bverse1,bverse2)
($bbook$ $bchapter$, $bverse1$ $bverse2$)
<$tiddler tiddler="$bbook$ $bchapter$">
<$list variable="line" filter="[all[tiddlers]fields[]nsort[title]]+[range[$bverse1$,$bverse2$]]">
<tr style="background-color:Ivory;">
<td style="color:blue;width:2em;text-align:center"><<line>> </td><td><$transclude field=<<line>> /></td>
</tr>
</
$list>
</$tiddler>
\end

But, sometimes the variable "bverse2" will remain empty, because only one single verse is needed. In this case, "range" will render an error, witch I'd like to avoid.

I'd like to have something like this:

if the variable "bverse2" remains empty, execute one block of commands, else go to a second bloc of commands.

I tried the SetWidget and the Conditional Variable Assignment, but I didn't manage to make it work. Can you point me in the right direction, please?


Martin Pusch

unread,
Feb 17, 2020, 9:48:46 AM2/17/20
to TiddlyWikiDev
After several hours of searching, reading and trying, I've solved my problem:

\define testmacro(param1,param2)
$param1$ $param2$
<$list filter="[[$param2$]match[]]" variable=nul>
(param2 is missing.)
</$list>
<$list filter="[[$param2$]!match[]]" variable=nul>
(param2 is present, the value is $param2$.)
</
$list>
\end

The call:

<<testmacro 1>>
<<testmacro 1 2>>

renders as:

1 (param2 is missing.)
1 2 (param2 is present, the value is 2.)

Martin


Hans Wobbe

unread,
Feb 17, 2020, 10:56:36 AM2/17/20
to TiddlyWikiDev
 Neat!  Thanks for sharing.

Regards,
Hans

Julio Peña

unread,
Feb 17, 2020, 2:27:17 PM2/17/20
to TiddlyWikiDev
Hello Martin and all,

Thanks for sharing this. It inspired me to experiment a bit to see what makes it tick.
Check out the link to my wiki (w.i.p.):


Look for the "Featured Samples" tab.


Best wishes,

Julio

@TiddlyTweeter

unread,
Feb 27, 2020, 1:51:21 AM2/27/20
to TiddlyWikiDev
Julio

Nice TW!

I like the fact its a series of small variations from "empty" that build to very good utility.

Best wishes
TT

Julio Peña

unread,
Feb 29, 2020, 5:19:27 PM2/29/20
to TiddlyWikiDev
Hey there TT,

Thanks!

All the best,
Julio
Reply all
Reply to author
Forward
0 new messages