Help for variables in transclude

I define a macro to replace variable, then call leafletmap widget. However it is working in one case, but not in another case. Appreciate for any advices.

This one is working which I replace name in substring of places

\define places(name)
<$leafmap places='{"filter":"[[$name$]] +[has[point]]"}'  cluster='25' zoom="8" height="300px" />
\end

<$set name="ptitle" value= {{!!title}}>
<$transclude $variable="places" name=<<ptitle>>>
</$transclude>
</$set>

But the following one is not working for leaflet map. The only difference is I move filter from macro to transclude.

\define places2(filter)
<$list filter="$filter$">
<$link />
</$list>
<$leafmap places='{"filter":"$filter$"}'  cluster='25' height="300px" />
\end

<$set name="ptitle" value= {{!!title}}>
<$transclude $variable="places2" filter="[<ptitle>] +[has[point]]"/>
</$set>

The list widget is working to list the expected items, but leafmap is not working.

See a minimum example: https://leafmap-test.tiddlyhost.com/. I expect to generate same maps.

Any ideas?

I’m not familiar with the leaflet plugin, but I think I know what the problem is.

In the first example, the $name$ in the leaflet widget is substituted by the macro, so it all works.

In the second example, transcluding the macro results in the following code:

<$set name="ptitle" value= {{!!title}}>
<$list filter="[<ptitle>] +[has[point]]">
<$link />
</$list>
<$leafmap places='{"filter":"[<ptitle>] +[has[point]]"}'  cluster='25' height="300px" />
</$set>

Note that the <ptitle> appears to be a variable in a filter expression. But in reality it is just a quoted string, which is further interpreted by the plugin, so the <ptitle> does not work as a variable.

You would need a second parameter to the places2 macro, to also pass and substitute the ptitle inside this string in the plugin widget.

And, as a general remark, I would advise not to rely on the text substitution mechanism and macros if possible, it leads to these kinds of problems. It’s better to use the procedures, a modern alternative to macros.

1 Like

I don’t follow this completely, One thing I know is you are using the older, deprecated macro syntax \define and then using the new syntax <$transclude $variable="places2" Perhaps you should change your macro to a \procedure then change the way you reference variables, within that procedure to <<paramname>> and try again.

Also if you can construct what you need from a filter, use that filter in a \function then use that to provide the attribute value instead. Although your use of the backtick attribute is similar. Its making things more complex, because now you call to leafmap is inside your macro definition.

  • Macros and procedures can not always be used as attribute values without first wikifying them, functions can because they are automatically evaluated.
  • Unfortunately I don’t have leafmap installed and don’t use it, so It would be hard for me to rewrite and test.
  • Perhaps you could rewrite this example, to see if you can reproduce the problem in a simpler example without the leaflet plugin, that runs on tiddlywiki.com?
    • I often find when I rewrite such things to share with people, I find the solution as I do it, and learn more at the same time.

Here is a guess I cant test;

\function places() [<name>has[point]addprefix[filter:]]

<$set name="ptitle" value={{!!title}}>
<$leafmap places=<<places>>  cluster='25' zoom="8" height="300px" />
</$set>
1 Like

Hi @TW_Tones and @vilc Thanks for your tips.

I sorted out with a non-smart way through generating filter string by set widget.

<$set name="ptitle" value= {{!!title}}>
    <$set name="filter" filter= "[<ptitle>taggingtree[]tag[Place]] [<ptitle>]  +[!is[system]!has[draft.of]]">
        <$transclude $variable="leaflet-place" filter=<<filter>>/>
    </$set>
</$set>