HowTo - Modify Contents Of Another Tiddler

There are many belt levels in Karate. Here are the belt levels in TiddlyWiki:

1 No Clue Of What Is Going On
2 Figured A Couple Of Things Out
3 Almost Getting The Hang Of It
4 Getting The Hang Of It, I Think
5 Wow, This Is Easy! No Wait, Why This Is Not Working??
6, 7, 8, Able To Help Others
9 and 10 Mastery, But Still Clueless

I proclaim myself to be at level 2. So with that out of the way, I was trying to figure out how one tiddler can change another tiddler. I know the information is out there, but for my peanut-sized brain, it’s hard to navigate. So here is what I found out so far.

1 Create the TargetTiddler

  • Open your TiddlyWiki.
  • Create a new tiddler and name it TargetTiddler.
  • In the body of TargetTiddler, write No.
  • Save this tiddler.

2 Create the ControlTiddler

  • Create another tiddler and name it ControlTiddler.
  • Add the following code into the body of ControlTiddler:
   <$button>
       Set Target to "Yes"
       <$action-setfield $tiddler="TargetTiddler" $field="text" $value="Yes"/>
   </$button>

3 Save this tiddler.

Try it out an play with it for a while.

Pro Tip #1:
The $action-setfield widget can also set fields using a more compact syntax:

<$action-setfield $tiddler="TargetTiddler" fieldname="fieldvalue"/>

The only time you really need to use the full $field="..." $value="..." syntax is when the field name contains spaces or is using a variable (i.e., $field=<<somevar>>).

So, for your ControlTiddler example, you could have written:

<$action-setfield $tiddler="TargetTiddler" text="Yes"/>

Pro tip #2:
The compact $action-setfield syntax also allows you to set multiple fields at the same time… like this:

<$action-setfield $tiddler="TargetTiddler" text="Yes" caption="caption text" otherfield="somevalue" />

Pro tip #3:
The value you set can come from a variable (i.e., fieldname=<<somevar>>) or even a “filtered transclusion” (the tripled curly brackets syntax). For example, to create a button that toggles the stored text between “No” and “Yes”, you could write something like this:

<$let target="TargetTiddler" newvalue={{{ [<target>text[No]then[Yes]else[No]] }}}>
<$button>
   Set <$text text=<<target>>/> to <<newvalue>>
   <$action-setfield $tiddler=<<target>> text=<<newvalue>>/>
</$button>
</$let>

Notes:

  • Using $let to define the target tiddler title makes it much easier to change the desired title without having to edit all the references to it. We can also use the same $let widget to compute the desired target text value, based on the currently stored value. Note that if the target tiddler doesn’t exist yet, this value will default to “No” and the $action-setfield will actually create the target tiddler for you.
  • The $button “label” uses a $text widget to display the target tiddler title. This ensures that it wiill be rendered as plain text rather than an embedded link to the target tiddler, so that clicking on the button doesn’t accidentally take you to the other tiddler.

enjoy,
-e

7 Likes

In Narenj plugin, there is a side editor and allow you to edit Another Tiddler there!
While the tiddler can be viewed in story river!