Reveal if variable has prefix?

Hy,

I have tiddlers that contain various fields ending with the same prefix (_name). A table list all these prefix (select), 1 per line, display some related informations in differents rows, and show a “delete entry” button at the end of the line.

But these fields are spread in two categories, pre-registred entries, with fields names prefixed with “pre_” and user entry, prefixed with “user_”.
My objective is to hide the “delete entry” button if the field start with “pre_”

I have had some success with this code, as it do display the button only when the fields doesn’t have the prefix “pre_” :

<$list filter={{{ [<select>!prefix[pre_]] }}} >
  <$button>
    remove entry
    <$action-deletefield $tiddler=<<currentTiddler>> $field=<<select>>/>
  </$button>
</$list>

But one problem remain :
Some fields have spaces in their name, and in this case, the button will be displayed multiple time. For example a field name of “user_William of Orange” will list 3 fragments separated by empty spaces, and thus display the button 3 times.
I tried a few things, especially to limit the number of entries in the list with multiple filter operators (limit, first, nth), but without success.

Would you know of an other way to solve this problem of surplus buttons ?

Your existing $list widget filter is using filtered transclusion (the triple curly braces), which is resulting in “double evaluation”…

Let’s suppose that <select> contains a field name such as “foo bar baz”. When the filtered transclusion is evaluated, it produces “foo bar baz” as a result. Then, because this result is used as the $list widget’s filter parameter, it outputs three buttons, one for “foo”, another for “bar”, and still another for “baz”.

To fix this, change:

<$list filter={{{ [<select>!prefix[pre_]] }}} >

to:

<$list filter="[<select>!prefix[pre_]]" >

-e

1 Like

Without knowing more context, this looks to me like a design antipattern.

IMHO “William of Orange” has no place in a field name at all.

A field name is an abstraction, it is a label for a property of an object if you wish. While “William of Orange” is the name of a single entity.

If I were you, I would rather design this like having tiddlers tagged “Person” or “User” for users. The next step would be to decide if “William of Orange” goes into the title field. Since hypothetically there are multiple persons with the same name, you may rather want to use some unique identifier as title and have a “name” field instead.

Then going back to your example, you will have just an “user” field in your tiddler which will hold that unique identifier (either the “title” or the “name” field of the “Person” tiddler).

Of course depending of the size of your codebase you may need to refactor a lot, so you may decide to rather not. But as your codebase will grow further, your codebase will only increase, thus the technical debt will grow.

Tiddler field names are keys of a dictionary (associative array). Generally keys are used to access values through them rather than parse their names because you choose to overload them (like “pre_” and “user_” which have different meanings).

In addition to the “double evaluation” problem I previously noted, I think there is also a problem with this line:

<$action-deletefield $tiddler=<<currentTiddler>> $field=<<select>>/>

The issue is that the action is enclosed within a $list widget that sets the currentTiddler variable to the current <<select>> value. As a result, the action is effectively:

<$action-deletefield $tiddler=<<select>> $field=<<select>>/>

which doesn’t seem to me to be what you would want.

To prevent the $list widget from changing the currentTiddler value, you can use a “throw away” variable like this:

<$list filter="[<select>!prefix[pre_]]" variable=none>

-e

1 Like

Thank you.
Your solution worked and you were right about the second one too. I had just replaced a set tiddler name with <<currentTiddler>> and hadn’t yet realized this problem.

These fields are generated from the name of other tiddlers, to import data they contains into a calculator.
User can create fields with spaces in their name, and so there is spaces in the names of the fields.
Theses fields are used for calculations, with multiple values being imported from other tiddlers. A result is calculated, and then, all the generated fields are removed, allowing the calculator to be re-used.

Having spaces in the names of the fields hadn’t been a problem until now.