Can I reference a data tiddler using a field value?

129 views
Skip to first unread message

amreus

unread,
Jan 18, 2020, 2:40:27 PM1/18/20
to tiddl...@googlegroups.com
Hi,

For example;

There exists a data tiddler named `data/28` with key/value pairs. Let's say `name` is one of the keys.

Another tiddler named "Display Tiddler" has a field `id` with value 28.

Can I get get a value from the data tiddler using the `id` field value?

This is an incorrect example, but maybe it shows my intent:

In the display tiddler:  `Name: {{data/{{!!id}}##name}}`

Hope it makes sense. Thanks.

Mat

unread,
Jan 18, 2020, 3:01:38 PM1/18/20
to TiddlyWiki
Sure, you could make a macro like so.

\define x(id) {{data/$id$##name}}

(put it either in the display tiddler or in a separate tiddler which then has to be tagged with $:/tags/Macro )

and then in the display tiddler, assuming you have an id field with value 28 (as you indicate in your example), you write

<$macrocall $name=x id={{!!id}}/>

Make up a better macro name than "x", of course.

<:-)

amreus

unread,
Jan 18, 2020, 3:25:34 PM1/18/20
to TiddlyWiki
Excellent, thanks for the example!

I realized I actually want something more general so that I can pass the data key as well. So this works:

\define x(id, key) {{data/$id$##$key$}}

Going to study up on macros... thank again.

amreus

unread,
Jan 18, 2020, 4:38:04 PM1/18/20
to tiddl...@googlegroups.com
Well, I'm not getting it.  I can't seem to create a macro without explicitly passing the `id` field value.

I would like to just do something like this:

\define fetch(attr) {{data/{{!!id}}##$attr$}}
...

Name: <<fetch "name">>


Is this explained more clearly somewhere? Thanks.

Mat

unread,
Jan 18, 2020, 6:13:00 PM1/18/20
to TiddlyWiki
\define fetch(attr) {{data/{{!!id}}##$attr$}}

First, note it is not possible to use nested "shortform syntax", i.e you must not {{...{{ }} ...}}

Instead use explicit transclusion for the outer one.

However, in this case you're also attempting to concatenate the id, which typically means a definition by itself, into so I'd instead suggest a two step macro:

\define fetch-inner(id,attr) {{data/$id$##$attr$}}
\define fetch(attr) <$macrocall name=fetch id={{!!id}} attr="$attr$">>

<<fetch "name">>

There may be better or more elegant solutions. 

<:-)

Mat

unread,
Jan 18, 2020, 6:14:27 PM1/18/20
to TiddlyWiki
Correction: That should be $name=fetch (note the $ sign)

<:-)

Mat

unread,
Jan 18, 2020, 6:18:25 PM1/18/20
to TiddlyWiki
Come to think of it, here's a slimmer variant:

\define fetch(attr, id:{{!!id}}) {{data/$id$##$attr$}}

<<fetch "name">>

This uses a fallback for the id parameter so unless you include an id value in our call, it'll use !!id

<:-)

TonyM

unread,
Jan 18, 2020, 7:11:03 PM1/18/20
to TiddlyWiki
Nice code Mat

Another option is to use the transclude widget instead of the short forms, which are after all compact ways of transcluding. The transclude widget allows tiddler and field parameters and can be read without deciphering curly braces $parms$ etc.. Ie no concatenation is necessary.

I see now that when we find ourselves concatenating strings just to make them work using short hand perhaps we should just use long hand and avoid concatenating all together.

With all the string operators now in filters using the triple braces to wrap a filter is another way to concatenate if necessary.

Regards
Tony

Mohammad

unread,
Jan 18, 2020, 10:49:17 PM1/18/20
to tiddl...@googlegroups.com
While Mat and Tony gave good solutions, I may recommend for learning dataTiddlers, read


By the way a simple solution may be

<$transclude tiddler={{{ [[data/]addsuffix{!!id}] }}} index="name" />


If you like macro and simple call to it then

\define fetch(key)
<$transclude tiddler={{{ [[data/]addsuffix{!!key}] }}} index="$key$" />
\end


and then

<<fetch name>>

I used key as input parameter as I think it is more semantic.

--Mohammad

Mohammad

unread,
Jan 18, 2020, 10:51:31 PM1/18/20
to TiddlyWiki
post edited: For email readers

TonyM

unread,
Jan 19, 2020, 12:45:21 AM1/19/20
to TiddlyWiki
Mohammad

Yes, thanks for expanding and giving examples. That's what I mean by;

With all the string operators now in filters using the triple braces to wrap a filter is another way to concatenate if necessary.

yony

Jim

unread,
Jan 19, 2020, 11:22:50 AM1/19/20
to TiddlyWiki
Thanks all for the help and hints.  Some of the examples I couldn't get to work, but finally managed to do what I wanted.

This is, at the moment, just experimenting with workflows and what's possible.  The under-lying ideas are separation of data from display, linking a tiddler to a data tiddler via an id number, and easier updating of data tiddler via external scripts instead of using fields which are embedded in a normal tiddler. 

Here is a simple demo in progress:  https://amreus.github.io/product-demo.html

Thanks again for the comments.



Reply all
Reply to author
Forward
0 new messages