[tw5] how to set a global variable with a tiddler field value

Hi,

I want to define a global variable in my script with the value contained into a tiddler field
When I hard code the global variable like

\define test_project() project-1

I can use the <<test_project>> variable without issue

But when I want this <<test_project>> to be initialised to a tiddler’s field like the following this does not work

\define test_project() {{{ [<<my_tiddler>>get[projectId]] }}}

Any “magic” idea …??

Thanks a lot for your support

Regards vpl

vpl,

First of all, the syntax for incorporating a variable into a filter expression is not <> but :

\define test_project() {{{ [<my_tiddler>get[projectId]] }}}

However, this still may not work, depending on how you use the macro. It’s tempting to think of macro transclusions as “function calls”, where when you say <<test_project>> you get the result of evaluating their contents as wikitext. But this is not how macros work – instead, the exact text of the macro (with any $text substitutions$ the macro might include) is inserted wherever the <> is used, which in this case is a filter expression in triple curly braces.

Now, this will still work fine if you just say, for instance:

<<test_project>>

In this case, TW replaces <<test_project>> with {{{ [<my_tiddler>get[projectId]] }}}, then sees that wikitext in the body of a tiddler, evaluates it, and gets the project ID. But it won’t work if you try to use it within another filter expression, or as the value of an HTML or widget attribute, e.g., suppose you had a tiddler whose title was this project ID and wanted to transclude it:

<$transclude tiddler=<<test_project>>/>

In this case, TW will be looking for a tiddler called “{{{ [<my_tiddler>get[projectId]] }}}”, and it presumably won’t find it.

If you need to, you can get around this with the $wikify widget:

<$wikify name=“myProjectId” text=<<test_project>>>
<$transclude tiddler=“myProjectId”/>
</$wikify>

In this case, you explicitly tell TW to evaluate the wikitext “{{{ [<my_tiddler>get[projectId]] }}}”, then use the result to transclude the appropriate tiddler.

You can read more on this in the Wikification section of Grok TiddlyWiki.

1 Like

Thanks a lot for this detailed answer which is going beyond my current expertise and understanding of these languages suptilities
In fact my problem was linked to a button action and thanks to It's About Time! — TiddlyTools: "Small Tools for Big Ideas!" (tm) code sample I was able to find a work an option

\define tasks_delete()
<$vars projectId={{{ [get[projectId]] }}}>
<$reveal default=“show” type=“match” text=“show”>
<$button class=“tc-button tt-button” tooltip=“delete this alarm”> {{$:/core/images/delete-button}}
<$action-listops $tiddler=<> $field=“tasks” $subfilter="-[[$(this_alarm)$]]" />
</$button>
</$reveal>
\end

The $reveal seems to be mandatory to get access to my projectId variable withing the button action-listops

Thanks a lot for your help

Regards

Vpl

Here’s a trimmed down version that eliminates the unneeded excess syntax:

\define tasks_delete()
<$button class=“tc-button” tooltip=“delete this task”> {{$:/core/images/delete-button}}

<$action-listops $tiddler={{{ [get[projectId]] }}} $field=“tasks” $subfilter="-[[$(this_alarm)$]]" />
</$button>
\end

Notes:

  • The $vars is not needed. Instead, use $tiddler={{{ [get[projectId]] }}} directly in the $action-listops widget
  • Eliminate the $reveal and /$reveal. Quite obviously, “show” will always match “show”, so this does nothing.
  • Remove “tt-button” from the $button class="…"; this is a TiddlyTools custom classname that is almost certainly not defined in your TiddlyWiki.
  • Fix the tooltip text to say “task” instead of “alarm”
  • $(this_alarm)$ references a variable defined outside the tasks_delete() macro and should probably be changed to whatever variable holds the name of the task you are deleting.

-e

Hi,

Much much simplier … indeed
I’ve turned around this simple solution for some time without finding it …

Thanks a lot for your very quick help

Regards