Converting a tiddler name to a field name to display relevant fields

138 views
Skip to first unread message

Mark Green

unread,
Jul 7, 2020, 6:45:53 PM7/7/20
to TiddlyWiki

I'm trying to specify a macro that will search all tiddlers for a field describing how they modify another tiddler (by default the current one) and list them together with the contents of those fields. This is my attempt, but it seems to produce no output, not even the output of the mfield variable at the bottem of the table.

\define ModifierBlock(for:"")
<table>
<$set name="x" value=<<__for__>> >
<$set name="mfield" filter="[title<x>lowercase[]addprefix[mod-]]">
<$list filter="[all[tiddlers]has<mfield>]">
<tr><th><<currentTiddler>></th><td><$view field=<<mfield>> /></td></tr>
</$list>
<tr><th><<mfield>></th></tr>
</$set>
</$set>
</table>
\end

In addition I notice that fields cannot contain spaces. There does not seem to be any filter operator to remove all spaces from a title to create something that could be used as a field name, is this possible?

Felicia Crow

unread,
Jul 8, 2020, 5:21:09 PM7/8/20
to TiddlyWiki
Hi,

your macro works fine for me, except when not specifying a tiddler, as your actual default value is an empty string, not the current tiddler. As a fix you could use the emptyValue parameter of the set widget when setting x:

<$set name="x" value=<<__for__>> emptyValue=<<currentTiddler>> >


For your second question: While there is, as far as I am aware of, no single filter operator to remove spaces between words you could play around with split[] and join[].
As an example this would replace the spaces with underscores when setting mfield:

<$set name="mfield" filter="[title<x>lowercase[]split[ ]join[_]addprefix[mod-]]">


Hope this helps you find a solution that works for you.

Regards,
Felicia

TW Tones

unread,
Jul 8, 2020, 7:12:56 PM7/8/20
to TiddlyWiki
Mark,

In addition I notice that fields cannot contain spaces. There does not seem to be any filter operator to remove all spaces from a title to create something that could be used as a field name, is this possible?

In the next version of tiddlywiki previewed here 
new slugify Operator and duplicateslugs Operator for generating human readable filenames/URLs 
This is also I believe going to support generating fieldnames as well.

However split and join as Felicia said, can do this if the title is not complex. Sometimes in such manipulations you may need to us the trim operato to remove leading or trailing whitespace and use the "\whitespace trim" pragma inside macro definitions.

Perhaps you could expand on this description, I had a little difficulty following
will search all tiddlers for a field describing how they modify another tiddler

And I/we can help further.

Regards
TW Tones

Mat

unread,
Jul 8, 2020, 7:59:09 PM7/8/20
to TiddlyWiki
As TW Tones notes, the coming slugify operator will do this but it is not sure that period characters (.) will be dealt with so you'd still need e.g split[.]join[-] 

Also note the "lowercase filter operator" because field names are not allowed to contain capital letters.

<:-)

Mark Green

unread,
Jul 9, 2020, 3:10:14 PM7/9/20
to TiddlyWiki


Thanks for your help. Basically there are two steps to the process.

1. Implement mod- fields: each tiddler will list the tiddler name and field contents for any other tiddler with a "mod-" name matching it. So if tiddler Foo has a mod-Bar attribute of "Hello", then viewing tiddler Bar will display a table including "[[Foo]] - Hello".

2. Implement mod inheritance: if a tiddler has an "inherit-mod" field that is a title list, it will also display all mod-fields relevant to other tiddlers named in that list.
So if tiddler Qux has an inherit-mod field naming "Bar" then Quz will also display the table showing "[[Foo]] - Hello" because although Foo does not modify Qux, it modifier Bar, and Qux inherits Bar.

The 2 was the reason for wanting to use a macro giving the page name with the current tiddler as the default, since I planned this would then be followed something like this (just an indication, not correct syntax or anything):
\define recursiveModifierBlock(for:"")
<<modifierBlock $for$>>
<$list filter="list[inherit-mod]" variable="parent">
<<recursiveModifierBlock <parent> >>
</$list>


Mark Green

unread,
Jul 19, 2020, 1:49:54 PM7/19/20
to TiddlyWiki

I don't suppose anyone has any further enlightenment on this, do they?

I'm still completely stuck. Even changing the macro to:

\define ModifierBlock(for:"")
Hello

<$set name="x" value=<<__for__>> >
<$set name="mfield" filter="[title<x>lowercase[]addprefix[mod-]]">
<<dumpvariables>>
<table>

<$list filter="[all[tiddlers]has<mfield>]">
<tr><th><<currentTiddler>></th><td><$view field=<<mfield>> /></td></tr>
</$list>
</table>
</$set>
</$set>
</table>
\end

Invoking

<<ModifierBlock test>>

In another tiddler produces nothing at all (not even "Hello" or the output from dumpvariables) and no indication why not, which is not really a good development environment! Is there any way to get a clue what's happening?

Felicia Crow

unread,
Jul 19, 2020, 3:39:44 PM7/19/20
to TiddlyWiki
Hi,
I am sorry, I have missed something that is by now obvious to me, but not necessarily to everyone.

When you wrote that your macro did not produce any output I tested the macro and since my tests where in the same tiddler due to it being easier it of course worked.
From what you are describing now it sounds more like not the macro itself being the problem, but that you are missing the tag $:/tags/Macro on the macro containing the tiddler.
Without this tag your macro only exists within the context of the tiddler it was defined in and can only be used there.

For the sake of completeness: There is also the ImportVariableWidget/Import Pragma, but judging by your description in this case it is probably easier to just mark your macro as global.

Regards,
Felicia

Mat

unread,
Jul 19, 2020, 4:46:58 PM7/19/20
to TiddlyWiki
 
\define ModifierBlock(for)
Hello
<$set name="mfield" filter="[[$for$]lowercase[]addprefix[mod-]]">

<<dumpvariables>>
<table>
<$list filter="[all[tiddlers]has<mfield>]">
<tr><th><<currentTiddler>></th><td><$view field=<<mfield>> /></td></tr>
</$list>
</table>
</$set>
\end

<<ModifierBlock test>>

So, IMO this slightly modified code should say Hello, list the dumpvariables and then show a table with tiddlers that have a mod-test field and their content of that field.
As Felicia says, either you have the macro and the call in the same tiddler or, if separate, then the macro must have the tag $:/tags/Macro

<:-)
Reply all
Reply to author
Forward
0 new messages