How to do substitution with a "local"❓ variable

Hi,

I want to contstruct a complex string(namely content at ② and ④) with a variable(namely tocTag at ①), which is read from another tiddler. I tried several ways however none of them works.

  • In line ②:the variable tocTag is not passed into formatTemplate macro
  • In line ④:the variable tocTag is not be replaced either

And I know $xxx$ is used to substituted variables defined in parameters list, it is just an example in my code. So I want to know:

  1. Any way to pass a variable into a macro, or
  2. Any way to replace placeholder with a “local” variable, or
  3. Is there a more simpler way to solve this problem

Thanks

\define lingo-base() $:/language/Sidebar/TabManager

\define formatTemplate(tocTag) 
    <div class="tc-table-of-contents">
    <<toc-selective-expandable "$tocTag$">>
    </div>
\end

\define add-tab-action()
<$list filter="[<stateTiddler>get[tabName]trim[]!is[blank]]" variable="ignore">
    <$set name="tocTag" filter="[<stateTiddler>get[tagName]]">    # ① got tagName and assign to tocTag
    <$set name="content" value=<<formatTemplate $tocTag$>> >      # ② I want pass tocTag to another macro, however this won't work
        <$action-createtiddler
            $basetitle={{{ [<stateTiddler>get[tabName]] }}}
            $savetitle="$:/temp/Sidebar/TabManager/new-tab"
            text=<<content>>                                      # ③ this works, however the content is wrong...
            text="....$tocTag$..."                               # ④ this won't work, anyway to do substitution with the `content` variable
        />
    </$set>
    </$set>
</$list>
\end

In your example in add-tab-action you are using $tocTag$ which is not a named parameter, nor do you show the macro being invoked.

Within a macro the value of a variable can be substituted with $(varname)$ but since it is substituted make sure you delimit it such as “$(varname)$”.

  • This refers to variables set outside the macro, otherwise if set in the macro you must use <<varinmacroname>>

Alternatively you can use a filtered transclusion to concatenate using filters which allows variables and more to be used.
eg {{{ [<varname>] }}}
or {{{ [addprefix[-]] }}} or {{{ [<varname>] [[--]] [{!!fieldname}] [[$prmname$]] +[join[ ]] }}}

There is some work underway that will extend the ways to substitute so keep your eyes open in new releases.

$(varname)$ can be used to substitute variables defined outside the macro. So you can change the formatTemplate macro to substitute the variable as defined outside the macro.

Try something along these lines:


\define lingo-base() $:/language/Sidebar/TabManager

\define formatTemplate() 
    <div class="tc-table-of-contents">
    <<toc-selective-expandable "$(tocTag)$">>
    </div>
\end

\define add-tab-action()
<$list filter="[<stateTiddler>get[tabName]trim[]!is[blank]]" variable="ignore">
    <$set name="tocTag" filter="[<stateTiddler>get[tagName]]" select="0"> 
        <$action-createtiddler
            $basetitle={{{ [<stateTiddler>get[tabName]] }}}
            $savetitle="$:/temp/Sidebar/TabManager/new-tab"
            text=<<formatTemplate>>
        />
    </$set>
</$list>
\end
1 Like

hi saqimtiaz,

I tried your solution, however it doesn’t work either. I guess the $(xxx)$ can only be used to substitute global variables.

Anyway , thanks

Hi TW_Tones,

I have also tried $(xxx)$, however it doesn’t work either. I guess that $(xxx)$ can only be used to substitute global variables.

And I also tried {{{ [[aaa]] [<tocTag>] [[bbb]] +[join[ ]] }}}, it produces a string aaa bbb without tocTag been formated.

Anyway, thanks

No that is untrue, however your example is incomplete, I can’t see where and how it is used, what uses the add-tab-action ? Nor can I replicate and edit this to help you. For example “get[tagName]” suggests you have a field named tagName do you?, where is it. Perhaps you mean <tagName>?

Could you make a bundle of tiddlers if installed on tiddlywiki.com I/we can troubleshoot with?, I assume you have modified it to @saqimtiaz suggestions?

Perhaps you could explain in plain English what you want and we can see if we can give you that?

  • A button when clicked retrieves a tagname from a state tiddler, then generates a tiddler that displays a table of contents based on that tagname and puts it in the sidebar?
    • Even this suggestion is incomplete because what set the state tiddler?

If you drop this tiddler tag-tagname.json (766 Bytes) in your wiki and rename it to a “tag: tagname” you also get a sidebar tab listing the items so tagged, try “tag: $:/tags/Macro”. This demonstrates some tricks I have discovered.

Hi, TW_Tones,

  • A button when clicked retrieves a tagname from a state tiddler, then generates a tiddler that displays a table of contents based on that tagname and puts it in the sidebar?
  • Even this suggestion is incomplete because what set the state tiddler?

This is very close to what I wanted. I’m implementing a SidebarTabManager, which facilitates adding/removing sidebar tabs. The following screenshot is a demo, and https://gist.github.com/Robin-Luooo/bfe215b57a97d680362c4c34a6103640 is my code (I’m blocked at creating sidebar tabs, and updating tab logic has not been implemented yet).

Thanks for your kindly help

Xnip2022-05-05_15-39-48

The problem I am seeing is that you are assigning the variable tocTag to the tagName field from the temp tiddler, however this tiddler has no such field.

Remember that temp tiddlers are not saved and do not survive a save and reload cycle.

Also suggest moving the add-tab-actions to the actions attribute of the keyboard widget.

Your welcome @GanG and welcome to the forums.

Good idea for a SidebarTabManager. Inspired by my guess as to what you want I just built a tool to add to my tag handling buttons something like what you were doing.

new-toc-tag-button.json (2.4 KB)

If you have a tiddler with a tag pill click and see the tag pill drop down where you can create a sidebar tab using this filter as he TOC

Snag_2058644

then in the sidebar

Snag_205ae4e

See $:/PSaT/new-toc-tag-button for where I implemented logic similar to your ideas here.