What do you recommend for naming variable in a $list widget when you do not need it?

There are situations where you do not need to use the variable of a $list widget, for example when using $list to check a condition and then run the remaining code.

I have learned from codes shared in the community patterns like below
[Updated based on following users reply]

i.

<$list filter="..." variable="null">
...

ii.

<$list filter="..." variable="ignore">
...

iii.

<$list filter="..." variable="">
...

iv.

<$list filter="..." variable="0">
...

v.

<$list filter="..." variable=".">
...

vi.

<$list filter="..." variable=ـ>
...

vii.

<$list filter="..." variable="none">
...

viii.

<$list filter="..." variable="dummy">
...

I myself like the third pattern <$list filter="..." variable=""> but I would appreciate developers to give their recommendation here.

1 Like

So, as you imply, it has to do with currentTiddler, i.e that if you want “currentTiddler” to still refer to the tiddler outside of the listwidget then you need to ensure that the list filter output doesn’t occupy this variable. Thus, to ensure that this is clear to the reader of the code that the filter output value is unimportant and can/should be ignored, then you name the output variable “ignore” or, as is popular in coding, “NULL”.

3 Likes

TW Core use variable="ignore" : https://tiddlywiki.com

So I guess this is the recommended approach. I like to write variable=_, it’s faster to type.

3 Likes

I think the best one is

<$list filter="..." variable="ignore">
...

I think there should be some examples in the core too.

2 Likes

I think the more verbose version is better for maintainability

1 Like

Your probably right ! It’s even possible to write even less code, like this :

<$list filter="..." variable>..</$list>

But it can be even more confusing, and searching for this is more difficult than searching for variable=“empty”.

2 Likes

UI was a variable=nul guy, now I am a variable=~ in dictionaries this often refers to the current word or root of the word so it seems useful. I like it because it seem not to impose too much and if I am not going to use the variable at all I am not sure how concerned people will be with its a use anyway.

  • However if there is a meaningful name I can give the variable even if I don’t plan to use it I still may. eg; <$list filter="[{$:/config/design-mode}match[yes]]" I may set the variable name to variable=design-mode`

I like this one, thanks for sharing!

I never remember which variable name the core uses in this case, so I often end up with variable="dummy" or variable="none" in my code…

Fred

1 Like

So I’ve typically not even included the variable parameter when I write a list widget. I’ve been assuming that it’s obvious that it would default to currentTiddler. Is this a bad approach? By doing it this way, am I setting myself up for problems?

There are two cases I can see were you may want to setup a variable and prevent the currentTiddler variable from being overwritten :

  1. You have several widgets inside your list widgets that are meant to modify the tiddler containing the list. In a lot of widget, the default value of the tiddler attribute is set to currentTiddler.

  2. You are using the list widget as a if/else toggle to hide some part of a tiddler, and in this case it’s better to keep the currentTiddler the same in order to access the fields of the original tiddler easily (e.g : {{!!title}} will be “wrong” if you do not set a variable).

In any case, the storyTiddler variable + the tiddler widget allows you to manually set the currentTiddler variable so there are always several ways to go around this issue.

3 Likes

I do write every list possible to use currentTiddler in part so all the macros and transclusions I develp use current tiddler, however I also use list rather than reveal widget for conditional display and as a result these dont use the currentTiddler but I do inside the list, thus must set another variable.

This is absolutely NOT a good practice. Leave the variable intact if you know you will use the currentTiddler variable generated by $list in any other case I would recommend to explicitly assign the $list variable name.

1 Like

So from reading the documentation, my take away was that if the variable attribute was used, then that variable would be assigned the list filter result and not the currentTiddler variable.

What kind of issues might arise if I don’t use the variable attribute? I understand about needing to know what the currentTiddler variable is and when, especially with nested lists. But if I have a single, unnested list then why would there be a need to use the variable attribute?

Sorry if I’m sounding a bit ignorant. I’m just a bit confused right now. Thanks for any clarifications.

@Mohammad … I think you did read the statement from @HistoryBuff wrong.

The post stated, that no variable parameter is used. That’s the default and so currentTiddler is used. IMO that’s perfectly fine. I only use the variable parameter, if I want to use a different name or if I want to “ignore” it.

First I was using _, however later I found that with multiple nested list it’s easy to name the variable, even if it’s not used, as it indicates what’s in the list.

Thank for all your inputs. I conclude the responses as below

  1. by default $list assigns the currentTiddler as its variable name, use it as long as it works for you
  2. name the variable of $list, when you need the currentTiddler defined outside $list (this prevents overwriting currentTiddler defined outside $list)
  3. use meaningful name for $list variable whenever possible and also when you do not use it
  4. the ignore is a name used by core when the $list variable is a dummy one (it is not used inside $list)
1 Like

Thanks @pmario, I feel better and less confused now.