External links from field values: is this state of the art?

Is this state of the art?:

\define more-link()
  <a href={{!!wikipedia-link}} class="tc-tiddlylink-external">More info from wikipedia</a>
\end

<!-- ... later ... -->

<<more-link>>

I feel as though I should be able to do this simply in wikitext without resorting to a macro, but I can’t find any way to do it.

To be clear, this is working code, and I can certainly live with it. But I would love to be able to skip the macro.

Macros, when made global with a tag such as $:/tags/Macro are actually quite sensible and allow you to invoke something where you want in your text, with that something, “the details”, not visible. “Write once, use many times”, you could say.

I have a tool in the view template which scans the fields on the current tiddler, looking for those fields who’s value begins with a prefix of a protocol eg http https. If one or more is found it presents them above the body text in this form;

  • <a href=<<fieldvalue>> class="tc-tiddlylink-external"><<fieldname>></a>

Actually I add more features, but you get the idea?

  • Once designed I do not use wikitext
  • I Just place a link in any fieldname, and it appears on the tiddler.

But returning to your question simply transcluding a field containing a link will make a working link

  • {{!!wikipedia-link}}, use the Browsers developer tools inspect to see what results.
  • However a lot of links are long and ugly.

Perhaps you are looking for this, Linking In Wikitext look down at external links For example;

[ext[wikipedia|https://www.wikipedia.org/]]

Notice it resembles the normal [[name[tiddlername]]
Look at the result of the above with inspect, you get this;

<a class="tc-tiddlylink-external" href="https://www.wikipedia.org/" rel="noopener noreferrer" target="_blank">wikipedia</a>
  • The above is the standard external link that tiddlywiki produces

But you want the link to be sourced from a field value

  • <a class="tc-tiddlylink-external" href={{!!wikipedia-link}} rel="noopener noreferrer" target="_blank">wikipedia</a>
  • Maybe just use a macro or view template?

Another way to look at macros is simply, reusable content, if in one tiddler you are going to use the same wikitext, with or without parameters, more than once, then define a macro in that tiddler.

  • If you will need to use it in many tiddlers make a global macro.

Yes, thank you. I do make significant use of macros already. Here it was a single template (applied to multiple tiddlers) and I was frustrated in many attempts to make it work. I can’t recall all the iterations I went through, although I’d swear I tried one of your suggestions below to no avail. Eventually, I gave up and wrote a macro. I just assumed that I was missing something basic.

Oh, yes, I looked this up and noted that it wouldn’t seem to handle a field.

I swear I tried this and it didn’t work. It was the first thing I did (without the class or rel attributes) and then I started digging into more arcane ideas. I’m going to have to go back and check it again.

Yes, this is a very familiar concept for a programmer. The hardest problem is to remember to avoid thinking of macros as functions. It’s a terribly easy mistake.

As always, thanks for the help.

When you said;

Were you then referring to;

That is wikitext in a template?

  • If so it is in some ways a different question.

Because in a way using a template is reusable code, just as macros are (and can include macros).

If this is the case;

  • Then we want to write wikitext that refers to the current tiddler. It is hard coded in the template, not the tiddler.

You can use this directly in the “view” and other templates;

<a href={{!!wikipedia-link}} class="tc-tiddlylink-external">More info from wikipedia</a>
  • The current tiddler is implied before the !!

or this

<$list filter="[all[current]has[wikipedia-link]]" variable=~>
   <a href={{!!wikipedia-link}} class="tc-tiddlylink-external">More info from wikipedia</a>
</$list>

or just {{!!wikipedia-link}}

and other forms,

  • What do you mean by this? I don’t seem to have this problem. “macros is macros”.

There are many ways and tangents to answering your initial topic, with all due respect, I suspect you are missing a particular concept, or cognitive model (or I am about your words :nerd_face: ).

  • I faced a number of these cognitive hurdles learning tiddlywiki myself. Once overcome they are incorporated into your thinking and you forget they were ever an issue. So they are hard to “share” with others.
    • If we can document these they would be “documentation gold”.

Grok TiddlyWiki goes over a few different way to create external links:

Most can be tweaked to incorporate a field to provide part of, or the entirety of, a web address.

Edit:
If you are just looking for a simple bit of wikitext in a ViewTemplate that includes a field value in an external link you can try this:

ExternalLink

Just in case it is of any use.

These are my Wikipedia links embedded in my tiddlers:

{{Control_flow||tWm}}

(“Control_flow” is not a tiddler. It is just part of the url used by the template to create the link https://en.m.wikipedia.org/wiki/Control_flow.)

The tWm template tiddler:

The result:

No kidding it is an easy thing to forget.

Think of a macro reference (don’t think of it as a “macro call”) as a meta-programming instruction (a pre-rendering instruction) to replace the reference with the snippet of text defined by that macro.

It is just a form of transclusion that is almost like a “mail merge.” The snippet of text defined by the macro is like a standard form letter, and the parameters are like the individual addresses.

That helps me get my thinking straight. I sure hope it doesn’t confuse the daylights out of anybody else !

Right, I know that intellectually, but I still continually want them to act like functions, and for wikitext to act like a programming language when it’s really a (very sophisticated) markup language. I’ll get there eventually, I’m sure, but it sometimes seems frustratingly slow.

I’ve heard enough other discussions here to know that this seems to be a common problem with programmers learning TW. I get that, but it doesn’t really ease the pain.

Thanks for your reply. It’s good to know others have gone through this too.

More in line with the OP:

(You can see the final results at BAM Docs — User Guide and Programming Reference for BASIC Anywhere Machine.)

In the URL, I only put the “book” part of the URL. “IA::” indicates the book is at the Internet Archive.

The {{||tBib}} transclusion will show the title of the book followed by a little link to the Internet Archive (or whatever source; at the moment, I’m only linking to the internet archive).

The tBib template tiddler (Some of my tiddler titles have suffixes which I precede with a unicode “#” character. I filter out that suffix for displaying.)

It was reviewing GTW that made me switch to a macro when other things weren’t working.

The trouble is that this was simple enough to do, but I was expecting something simpler still, and when that didn’t work, it seems like I overshot.

Tony’s suggestion of

<a class="tc-tiddlylink-external" href={{!!wikipedia-link}} rel="noopener noreferrer" target="_blank">
  wikipedia
</a>

worked just fine.

This will probably look more understandable after a night’s sleep. My brain’s not up for it now. I’ll try again tomorrow.

1 Like

Yes, sorry if I didn’t make that clear. My fundamental question has been answered aptly by you and by several others. No: that is not the state of the art. It’s overcomplicated. For some reason, it seemed that everything else I tried that wasn’t a macro call from a template was failing. But one of your solutions did exactly what I wanted to without that macro, and with simpler code than I had written so far.

A section of GrokTiddlyWiki describes this well. I’ve reread it multiple times but it clearly still hasn’t sunk in. For me a function is a callable bit of code that at runtime offloads some of the common work into one place. A macro instead (and I’ve use LISP-style programming languages which have powerful macro systems) simply imports a chunk of work into your current workflow. These are distinct, but related processes.

I get the impression that this cognitive problem is one that affects many programmers trying to learn TW.

Oh, I’m quite certain that’s the case. I describe my problems as being confused by syntax, but I know that truly it’s fundamental concepts I still don’t have down.

Over time, “regressing” (i.e. the semantic meaning of macro falling back to the meaning in some non-TW context) becomes far and fewer between.

The most important thing you can do is banish the term “macro call” and “macro calling” from your vocabulary and communications/notes/whatever about TiddlyWiki.

Find alternative terminology that reminds you best what TW macro is and does. For me, it is “macro reference” (which reminds me, strangely, of macro substitution/transclusion).

Interestingly for me, an given I have used a range of macros over the years, inside Microsoft OS and applications, command line coding, keyboard macros etc… when someone says macros I ask “what kind?”.

One day I came across “tiddlywiki macros”, and whilst I had some preconceptions of macros, I quickly lost that, and since then I have learned “TiddlyWiki Macros” are their own thing.

For some reason when people say things like “TiddlyWiki Macros are not real macros, functions or like programming”, or are “a (very sophisticated) markup language” etc… I get a little agitated,

I am not agitated with the person (they are after all just, trying to understand and need to “see it” based on the axioms they are familiar with) but agitated that these descriptions, are ultimately miss leading. I feel like stepping in and putting a nuanced argument, that the “statement is not quite correct”. But then it would be like arguing against shadows, the shadows being peoples “transient rationalisations”. So I let those statements stand unchallenged.

My agitation comes from the fact there remains a lot of different “conceptualisations out there in the community”, many of which are not quite right, but necessary on their journey, but in many ways, nothing can be done about that.

  • I expect some of my “conceptualisations” are wrong too.

Here is my first post leveraging ChatGPT;

How do macros differ from other coding?

Macros are a type of programming construct that are used to generate code at compile time. Macros are a feature of many programming languages, including C, C++, and Lisp.

The main difference between macros and other coding is that macros operate on the source code itself, while other coding constructs operate on the runtime environment. Macros allow you to manipulate the code before it is compiled, while other constructs are used to manipulate the program’s behavior during runtime.

In other words, macros are a way to programmatically generate code that is executed at compile-time, while other coding constructs are used to generate code that is executed at runtime.

Macros are often used to automate repetitive tasks or to generate code based on complex rules or patterns. They can be very powerful, but they can also be difficult to use correctly. Macro code can be difficult to read and understand, and can be prone to errors if not used carefully.

Overall, macros are a specialized type of programming construct that can be very useful in certain situations, but they require a good understanding of the programming language and careful use to avoid errors.

Reading this you see perhaps where the confusion comes from ;

  • TiddlyWiki is not compiled at all
  • In some ways it is not even interpreted
  • It is ultimately “rendered”, in keeping with it as markup.
  • It kind of has two runtimes, as the wikitext, widgets and macros are parsed, then a final parsing of that result into HTML.
  • Then to make it just a little more mind blowing changes are propagated throughout the whole wiki with an efficient “only refresh a minimum” mechanism.

But as above this is true for tiddlywiki.

macros operate on the source code itself

That ChatGPT thing, I can’t say I’m a fan.

I much prefer Macro (computer science) - Wikipedia

To me, the biggest problem with macros in TiddlyWiki is the documentation titled “calling macros”.
There is nothing being called. (That phrasing just triggers “call a function/procedure” thinking.)

Or Chat GPT …

1 Like

I just want to point out that it was not TW macros but wikitext that I described as “a (very sophisticated) markup language”. Maybe that will reduce your agitation a bit.

And I don’t think people are saying that TW macros are not real macros. They are very much akin to macros in C and in LISP. But for whatever reason (perhaps because there are no functions or procedures (or are they coming with parameterized transclusion?), some programmers latch onto them as the closest thing available. They overlap with function execution, but they are most definitely not the same.

I know the feeling. I’m a fairly senior-level programmer, and do a lot of training an mentoring, and it’s difficult to know when to go beyond, “Yes, that’s the general idea…” to “Sure, but technically, you also need to consider…”

I usually struggle quite a bit here before I ask questions. In the background, I’m also starting to work on my own TW documentation, meant for people who think like I do. I won’t be ready to share it for some months, but I hope it will be a useful resource. It’s not the sort of thing that would fit into the main TW page, but maybe parts of it will make it in there. Maybe, if all goes well, it can help the next programmers who come along with similar issues to mine.

I haven’t tried ChapGPT yet, but I’m reasonably impressed with this answer. I think it’s fairly accurate, especially the first two paragraphs. The last two sound more attuned to C/C++ than the other, more powerful and yet still more intuitive, versions in something like LISP. But the important point here for TW is the distinction between source code and runtime. The closest TW has to a runtime is rendering/rerendering, and I keep finding myself thinking of Macros operating during this, when in fact they happen earlier.

I’d argue that. Compilation is the process of converting from one language to another, usually from a higher-level language to a lower one. I think the wikitext → HTML/DOM transformation can reasonably be called compilation.

I personally do not like calling “WikiText” the whole gamut of features in TiddlyWiki.

I can’t help but consider, because it makes more sense to my big picture: “WikiText” to me is just the Tw-markdown, the non-visual widgets are Tw-script, the visual widgets are Tw-Gadgets (or Tw-Controls), and filtering operations are Tw-QueryLanguage (TwQL), and then TwPragmas (the meta stuff.)

I can’t explain it, but the clear delineation is very calming/soothing for me. Otherwise, I have the same kind of anxiety as the kid who doesn’t like the mashed potatoes touching the peas…

I did read through and understand your tBib template. Thank you for sharing. I had one quick question. I understand nth, and in fact often name a function that for a similar purpose. But using it without a parameter, as nth[], seems to be a much less explicit way of writing first[]. Does it offer any advantages over first[]? Did it simply predate first[]? Or am I misunderstanding your usage here?