Macro syntax: AI says, it is ok, but it is not

Hi,

I’m trying to replace a fixed path value by a transclusion with the help of the LetWidget.

PerplexityPro says, the code will be running, but it is not:

\define imagew(imgtitle,width)
<$let imgfolder="twImages/{{$:/SiteTitle}}">
<a href=<<imgfolder>>/$imgtitle$ title="Click to open image in a new tab" target="_blank">
     <img src=<<imgfolder>>/$imgtitle$ width="$width$" height=""/>
</a>
</$let>
\end

The former version of the macro using a fixed path is working well:

\define imagew(imgtitle,width,folder:"twImages\MyWiki")
<a href="$folder$/$imgtitle$" title="Click to open image in a new tab " target="_blank">
     <img src="$folder$/$imgtitle$" width="$width$" height=""/>
</a>
\end

Version: TW v5.3.5

I appreciate ideas how to improve the code :slightly_smiling_face:

Dont even bother with LLM’s for TiddlyWiki script yet. They a full of S–t.

I will look at you code tomorrow if you dont have an answer.

LLMs are bullshit machines. Not only wouldn’t I trust them to generate working TW code, I wouldn’t trues them for much of anything.

Here is one way to convert that to a more modern procedure and substituted attribute values:

\procedure imagew(imgtitle,width)
<$let imgfolder=`twImages/${ [{$:/SiteTitle}] }$`>
<a href=`$(imgfolder)$/$(imgtitle)$` title="Click to open image in a new tab " target="_blank">
     <img src=`$(imgfolder)$/$(imgtitle)$` width=`$(width)$` height=""/>
</a>
</$let>
\end

It’s tested, but barely.

1 Like

You can’t concatenate like this. It’s a common beginner’s approach, and it might be that the LLM trained off of those examples. The problem is that the LLM doesn’t know that it’s looking at bad data.

You can try instead:

folder=`twImages/${$:/SiteTitle}$`

Notice that backticks, not quote marks are used.

Hmm. Same thing with this

Try

src=`$(imgfolder)$/$imgtitle$`

Note the backticks again, and that the variable uses a parenthesis $(...)$
and not a curly bracket.

I don’t know if this will fix everything, but it’s a start.

Reference:

https://tiddlywiki.com/#Substituted%20Attribute%20Values

2 Likes

Thanks to both of you for your input :slightly_smiling_face:

This version is working
:partying_face:

\procedure imagew(imgtitle,width)
<$let imgfolder=`twImages/${ [{$:/SiteTitle}] }$`>
<a href=`$(imgfolder)$/$(imgtitle)$` title="Click to open image in a new tab" target="_blank">
     <img src=`$(imgfolder)$/$(imgtitle)$` width="$width$" height=""/>
</a>
</$let>
\end imagew

Some questions to understand the syntax:

  • \define instead of \procedure does not work

I’m not familiar with this syntax:

${ [{$:/SiteTitle}] }

:thinking:

Check out the documentation on Substituted Attribute Values. We put a variable name inside $( - )$ wrappers and a filter inside ${ - }$ ones. In this case, we just use the filter [{$:/SiteTitle}].

Procedures are the modern replacement for macros. There are some differences, especially around how you refer to parameters. (In procedures, it’s the same as any other variable; macros were more complex.) But they’re very similar.

One thing I noticed is that you changed width="$(width)$" to width="$width$", which uses the old macro syntax and won’t work here. But width=<<width>> will also work, and is arguably simpler. That’s one of the nice simplifications offered by procedures.

1 Like

Since you’re using $(imgfolder)$/$(imgtitle)$ in two separate places and imgfolder isn’t used anywhere else in the macro, you could further simplify your code a bit:

\procedure imagew(imgtitle,width)
<$let imgpath=`twImages/${ [{$:/SiteTitle}] }$/$(imgtitle)$`>
<a href=<<imgpath>> title="Click to open image in a new tab" target="_blank">
	<img src=<<imgpath>> width=<<width>> height="" />
</a>
</$let>
\end imagew

One big advantage of the backtick substitution syntax is that it lets us mix literal strings (like twImages/), variables (like $(imgtitle)$) and filter syntax (like ${ [{$:/SiteTitle}] }$) all within a single string. This means that you can skip the $(imgfolder)$ step and construct a single string <<imgpath>> which can be used in both places — and this will be slightly more efficient than performing the same $(imgfolder)$/$(imgtitle)$ concatenation twice.