[TW5] Best Solution to Similar Tiddlers

195 views
Skip to first unread message

Jeff AnCap

unread,
Jan 27, 2017, 6:20:01 PM1/27/17
to tiddl...@googlegroups.com
For the last 24 hours, I've been trying various methods to solve the following problem.  I want to have multiple tiddlers that have a similar format but with some variation (which I'll show below).  I've looked into templates, transclusion, DataTiddlers, and macros, and I haven't yet been able to put all the pieces together to achieve my goal.  I'm still very new to TiddlyWiki.  Here's a simple version of what I'm trying to achieve:

TiddlerFrodo
! Description
Frodo Baggins is a Hobbit who lives in a hole in the ground.

! Stats
Name: Frodo Baggins
Home: Hobbiton
Occupation: Ring-Bearer

TiddlerSamwise
! Description
Samwise Gamgee is a Hobbit who is in love Rosie.

! Stats
Name: Samwise Gamgee
Home: Hobbiton
Occupation: Gardner


The solution I've been trying, but have been coming up short with, is to have what I'm calling a "template" tiddler (but I'm not sure if that's what it really is TW) that contains the repetitive parts of the text above.  I'm defining a macro and using variables for the changing parts.


TiddlerTemplate

\define character(description, name, home, occupation)
! Description
$description$

! Stats
Name: $name$
Home: $home$
Occupation: $occupation$
\end

Next, I have a json data tiddler (I'm not sure if JSON or the dictionary tiddler is better) that looks like:


TiddlerData
[
 
{
   
"Description":"Frodo Baggins is a Hobbit who lives in a hole in the ground.",
   
"Name": "Frodo Baggins",
   
"Home": "Hobbiton",
   
"Occupation": "Ring-Bearer"
 
},

 
{
   
"Description":"Samwise Gamgee is a Hobbit who is in love Rosie.",
   
"Name": "Samwise Gamgee",
   
"Home": "Hobbiton",
   
"Occupation": "Gardner"
 
}
]

Finally, in TiddlerFrodo and TiddlerSamwise, I'm calling the character macro I defined, but I'm not sure how to get at the corresponding data in the data tiddler.  When it's just one set of data, I know I can get at it with something like {{TiddlerData##Description}}, but when I have multiple sets of data, I'm at a loss.  So, my questions are:

1. What's the best way to achieve my initial goal?
2. If what I'm doing is essentially fine, what's the syntax for grabbing the JSON data?

Thanks,
Jeff

Tobias Beer

unread,
Jan 27, 2017, 7:43:02 PM1/27/17
to TiddlyWiki
Hi Jeff,

Long story short, I don't think there currently is a module for grabbing data from json like I imagine you want it to.

Best wishes,

Tobias. 

Tobias Beer

unread,
Jan 27, 2017, 8:00:04 PM1/27/17
to TiddlyWiki
Asked differently,

What's your idea behind you wanting to use this kind of json? Why use it and does it have to be like this?

Fwiw, the json you gave as an example does not fit the basic json "data tiddler" format TiddlyWiki can read.

It resembles more the json produced when exporting tiddlers in json format,
except for the "field names" starting uppercase in your case.

Best wishes,

Tobias.

Jeff AnCap

unread,
Jan 27, 2017, 8:40:04 PM1/27/17
to TiddlyWiki
Thanks for the replies, Tobias.  I'm not committed to the method I've proposed, I was just really close to getting what I was looking for, and the JSON DataTiddler I had seemed valid because I saw that the History List was formatted similarly, to my eyes at least.  I'm still learning the basics.

So, to step back from the means I proposed previously, I'll see if I can clarify my intent.  I'm making a wiki for a PC game I'm working on.  As part of that, to display information about all the characters in the game, I'm planning on having at least 50 tiddlers that have the same format, similar to the TiddlerFrodo and TiddlerSamwise above, but with more data in each.  If I decide to change that format a little down the road, I'd rather change it in one place instead of the 50+ and not have to change the data I'm trying to plug in.  It seems like it would be simple to pull off, relative to all the other things I've seen that TW can do, so I'm sure I can accomplish it, it's just a matter of how.

Jeff

Tobias Beer

unread,
Jan 27, 2017, 9:06:37 PM1/27/17
to tiddl...@googlegroups.com
Hi Jeff,

You can use a JSON based workflow to do what you want, e.g.:
  1. open Advanced Search > Filter
  2. filter for your actual character tiddlers (however, they should have a proper tiddler syntax, fields and all)
    • if you want, you can add that filter to the presets
  3. then export those as json using the button to the right
  4. there's your JSON with all character data for you to batch modify in the appropriate editor
  5. when you're done, drop it back onto your wiki
However, notice how you will always only get lowercase keys in that JSON array of tiddler objects,
since tidddler fields are all lowercase, i.e. Description is description ...while Name should be title,
i.e.while you may want to change the field names,
there's one you probably don't want to change, i.e. the "title".

Best wishes,

Tobias.

Jed Carty

unread,
Jan 28, 2017, 3:42:21 AM1/28/17
to TiddlyWiki
I have done a lot of similar database-ish uses of tiddlywiki and here is my advice based on that:

1) Make lots of tiddlers. The data for each character can (and probably should) be its own tiddler. So if each character has a description, name, home and occupation than you make one tiddler per character like this (I would use fields instead of the json tiddlers because they are easier to work with in the rest of tiddlywiki):

tiddler title:
$:/data/Characters/Samewise Gamgee

tiddler fields:

description: Samwise Gamgee is a Hobbit who is in love Rosie.
name: Samwise Gamgee
home: Hobbiton
occupation: Gardner

Then to display the information you make a template tiddler like this, I am going to assume you make the tiddler title OMGACHARACTER because I want to:

!Name: {{!!name}}


!! Description
{{!!description}}

!! Stats
Name: {{!!name}}
Home: {{!!home}}
Occupation: {{!!occupation}}

and to display it you put this inside a tiddler:

{{$:/data/Characters/Samwise Gamgee||OMGACHARACTER}}

than it will display the template using the data for Samwise Gamgee.

Then to make life easier you can make some macros like this in a tiddler you tag with $:/tags/Macro:

\define DisplayCharacter(Character)
{{$:/data/Characters/$Character$||OMGACHARACTER}}
\end

\define DisplayCharacterStat(Character, Stat)
<$view tiddler="""$:/data/Characters/$Character$""" field=$Stat$/>
\end

To display a chracatres stat block use the `DisplayCharacter` macro

Usage: `<<DisplayCharacter "Samwise Gamgee">>`

Result:

<<DisplayCharacter "Samwise Gamgee">>

To display a specific stat for a character use the `DisplayCharacterStat` macro

Usage: `<<DisplayCharacterStat "Samwise Gamgee" "home">>`

<<DisplayCharacterStat "Samwise Gamgee" "home">>


and then to make life easier you can make a form that lets you input data for a character and it will automatically generate the character data tiddlers for you.

Thomas Elmiger

unread,
Jan 28, 2017, 4:03:43 AM1/28/17
to TiddlyWiki
Hi Jeff

If would suggest a strategy that is similar to Jed’s, but even simpler. I would make one tiddler for each character and tag them for example "role". The Name could be the title, the description could be stored in the text field. All other standard attributes would be in additional user fields like role-home, role-occupation and so on. Same as Jed suggests. I prefix my fields (role-) so they are listed together no matter if they start with a or z. (You will need at least one more field list-after for this method.)

The way to see this information would be to apply a conditional view template (links to a tutorial Tobias made) to all tiddlers tagged "role". The format of the template would be like Jed describes, but without the need to include the name as a title (it is allready the tiddler title) and possibly the description.

!! Stats
Name: {{!!title}}
Home: {{!!role-home}}
Occupation: {{!!role-occupation}}


If you later on decide to add an attribute like role-bestfriend you just create such a field for your next character, add it to the template and you’re done.

Main advantage: To find roles you can use all the standard ways TW offers (e.g. list by tag, search) in a standard manner with the role names as a meaningful result.
A form to create new tiddlers like this would be possible too.

All the best,
Thomas

Jeff AnCap

unread,
Jan 30, 2017, 4:31:45 PM1/30/17
to TiddlyWiki
It looks like this is going to work for me.  Thanks for the help everyone; I appreciate it!

Jeff
Reply all
Reply to author
Forward
0 new messages