TW Variables Have a Scope and can be "Stacked"

TW variables have a scope and can be “stacked”

The code snippets below only make sense, if you try them yourself!

There are several things in TW that change the currentTiddler variable

  • Directly setting currentTiddler with a $set or $let widget.
  • Using a $list widget to enumerate the outputs of a filter.
  • Transcluding another tiddler with the {{curly braces}} syntax.
  • Using a $tiddler widget.

Set-widget

As I wrote. Variables have a scope. They are valid inside the widget they are defined in.
Procedures and macro definitions are special cases of set-widgets.

Their scope is the whole tiddler.

title: procedures-are-variables

\procedure test() abc
\procedure test() xyz

```
<<test>>  ... Will be xyz 
```

<<test>>  ... Will be xyz 

So the last one wins. The above code is a “shortcut for”

<$set name=test value=abc>
  <$set name=test value=xyz>

  <<test>>

  </$set>
</$set>

Try this:

<$set name=test value=abc>

  <<test>>

  <$set name=test value=xyz>

    <<test>>

  </$set>
</$set>

list-widget

Let’s say I want to create a list of tiddler links, that use the name of the tiddler the code lives in and add “a”, “b” and “c” at the end.

title: myTemplate

\function concat() [<currentTiddler>] [<title>] +[join[-]]

<$list filter="a b c" variable="title">
  <$link to=<<concat>> /><br>
</$list>

By default the list-widget would “overwrite” the currentTiddler variable, that’s why we can provide a new variable name. In this case title

  • The list-widget iterates over a, b, c and sets the title variable.
  • The currentTiddler stays the same.

The currentTiddler variable is the tiddler-title, the code lives in.

The concat function can be implemented in several more ways, depending on the usecase.


Transclusion

If you create a new tiddler named: test … You can play with templates.

  • The first transclusion will set the currentTiddler variable to myTemplate
  • The second transclusion will pass the currentTiddler variable on to the template
  • The third example passe the name “xxx” to the template, which will be used as currenTiddler
title: test

{{myTemplate}}

---

{{||myTemplate}}

---

{{xxx||myTemplate}}

Docs about transclusion: https://tiddlywiki.com/#Transclusion%20in%20WikiText


About your code

First things first. Syntax.

There is a syntax error here. It’s not possible to have spaces between = and <<currentTiddler>>


  • In the example below the tiddler title is BLARGH
    • When a tiddler is rendered in the story, the UI defines the currentTiddler variable and sets it to the tiddler-title, so it can be used in the tiddler body text.
  • Then the set-widget form your code will set the this variable to <<currentTiddler>> which is BLARGH as defined above.
    • IMO you got that right, but IMO there was a syntax problem
  • Then the contains-operator will list all tiddlers that have a field named: worshipsGod
    • Internally the filter is automatically expanded to [all[tiddlers]contains:worshipsGod . . .
    • Then it will compare the value of worshipsGod field with the value of the variable <this>

title: BLARGH 

<$set name="this" value=<<currentTiddler>>>

BLARGH should be 'this' of course.

<$list filter="[contains:worshipsGod<this>!sort[at]]">
  <<currentTiddler>><br>
</$list>

</$set>

According to the documentation, the list-widget will use the variable <<currentTiddler>> inside its body.
But
That can be changed with the “variable” parameter. So you can choose any name, that fits helps you to make the code more understandable.

<$list filter="[contains:worshipsGod<this>!sort[at]]" variable="name-of-god">
  <<name-of-god>><br>
</$list>

You can use the attached JSON tiddlers and play with the examples above.
Playing around with the stuff should make things clearer

Hope that helps

Have fun!
mario

variable-scopes.json (1.1 KB)

4 Likes

This post may be relevant too, but it needs its context. So here is the link an not a copy: Closing <$set>, nested SetWidgets - #6 by pmario

2 Likes