Help with WikiText Macro

101 views
Skip to first unread message

Adithya B M

unread,
Aug 15, 2020, 9:21:39 AM8/15/20
to TiddlyWiki
Im trying to write a simple macro to: Return the contents of the field 'name' and if the field is absent, the title of the tiddler (in plain text). 

Can someone help me in getting this to work?

\define get_title()
{{{[all[current]get[name]else[!!title]]}}}
\end

Cheers,
Adithya

Mark S.

unread,
Aug 15, 2020, 9:34:53 AM8/15/20
to TiddlyWiki
{{{[all[current]get[name]else{!!title}]}}}

In filter expressions, use a single curly bracket set "{!!title}" when transcluding field values.

Adithya B M

unread,
Aug 15, 2020, 9:38:47 AM8/15/20
to TiddlyWiki
Thanks mark.

This solves the problem halfway. This seems to print the title as a link to the <currentTiddler>.

How to get the title in plaintext?

Cheers,
Adithya

Eric Shulman

unread,
Aug 15, 2020, 9:48:33 AM8/15/20
to TiddlyWiki
On Saturday, August 15, 2020 at 6:38:47 AM UTC-7, Adithya B M wrote:
This solves the problem halfway. This seems to print the title as a link to the <currentTiddler>.

Use the <$text> widget, like this:
<$text text={{{ [all[current]get[name]else{!!title}] }}} />

note also that you can use the currentTiddler variable in place of all[current] and {!!title}, like this:
<$text text={{{ [<currentTiddler>get[name]else<currentTiddler>] }}} />

-e


Adithya B M

unread,
Aug 15, 2020, 11:39:40 AM8/15/20
to TiddlyWiki
Hi Eric

Thanks for weighing in! 

Your reply solves that problem and allows me to present another one in the same vein :D
Please look at the code below. In this, the get-extract() should called if the description field is absent. How could this be made to work?

---
\define get_description()
<$text text={{{ [<currentTiddler>get[description]else<get-extract<currentTiddler>>] }}} />
\end

\define get-extract(title)
<$wikify name="output" text={{$title$}} mode="block" output="text">
<$text text={{{ [<output>split[]first[500]join[]] }}}/>
</$wikify>
\end
----


Cheers,
Adithya



Eric Shulman

unread,
Aug 15, 2020, 11:59:36 AM8/15/20
to TiddlyWiki
On Saturday, August 15, 2020 at 8:39:40 AM UTC-7, Adithya B M wrote:
Please look at the code below. In this, the get-extract() should called if the description field is absent. How could this be made to work?
\define get_description()
<$text text={{{ [<currentTiddler>get[description]else<get-extract<currentTiddler>>] }}} />
\end

\define get-extract(title)
<$wikify name="output" text={{$title$}} mode="block" output="text">
<$text text={{{ [<output>split[]first[500]join[]] }}}/>
</$wikify>
\end

Unfortunately, you can't invoke a macro with parameters from inside a filter.  Instead, you can "pre-compute" the extract value, and then use the filter, like this:
\define get_description()
<$wikify name="extract" text={{{ [<currentTiddler>get[text]split[]first[500]join[]] }}}>
<$text text={{{ [<currentTiddler>get[description]else<extract>] }}} />
</$wikify>
\end
Note that for the $wikify widget, mode="block" and output="text" parameters are the defaults, so they can be omitted.

-e

Adithya B M

unread,
Aug 15, 2020, 12:40:47 PM8/15/20
to TiddlyWiki
Brilliant! Thanks Eric.

Can you suggest any better way to extract the a "excerpt" of a tiddler?
I found this get_extract function searching this forum but it kind of breaks the text by eating the formatting html. I just want plaintext of the first 140 characters or so with non-relevant characters replaced by spaces (and multiple spaces trimmed to one).

Cheers,
Adithya

Eric Shulman

unread,
Aug 15, 2020, 1:15:46 PM8/15/20
to TiddlyWiki
On Saturday, August 15, 2020 at 9:40:47 AM UTC-7, Adithya B M wrote:
Can you suggest any better way to extract the a "excerpt" of a tiddler?
I found this get_extract function searching this forum but it kind of breaks the text by eating the formatting html. I just want plaintext of the first 140 characters or so with non-relevant characters replaced by spaces (and multiple spaces trimmed to one).

Perhaps if you wikify the *entire* tiddler text first, and then take the first 140 characters from that, you will avoid "eating the formatting".
\define get_description()
<$wikify name="out" text={{{ [<currentTiddler>get[text]] }}}>
<$vars  extract={{{ [<out>split[]first[140]join[]] }}}>

<$text text={{{ [<currentTiddler>get[description]else<extract>] }}} />
</$vars>
</
$wikify>
\end

-e

Mark S.

unread,
Aug 15, 2020, 1:47:33 PM8/15/20
to TiddlyWiki
@Eric

What I don't understand, is why this filter didn't work :

[{!!name}else{!!title}]

If name doesn't exist, then it doesn't display anything, despite the definition of "else"

if the list of input titles is empty then return a list consisting of a single constant string, otherwise return the original titles

Without "name", then {!!name} should be empty and the "else" operator should kick in. But it doesn't.

Eric Shulman

unread,
Aug 15, 2020, 2:36:52 PM8/15/20
to TiddlyWiki
On Saturday, August 15, 2020 at 10:47:33 AM UTC-7, Mark S. wrote:
What I don't understand, is why this filter didn't work :
[{!!name}else{!!title}]
If name doesn't exist, then it doesn't display anything, despite the definition of "else"

if the list of input titles is empty then return a list consisting of a single constant string, otherwise return the original titles
Without "name", then {!!name} should be empty and the "else" operator should kick in. But it doesn't.

The "else" filter only works if the preceding filter produces NO result... but if the {!!name} field exists, it has a value...  it's just blank text.

In contrast, note that the documentation for "get[...]" (https://tiddlywiki.com/#get%20Operator) says this:

If the corresponding tiddler contains field F, and the value of this field is not empty, then its value is appended to the output.

Thus, when you write "get[name]", if name is blank then there is no filter result -- as opposed to a value which happens to be blank.

-e

Mark S.

unread,
Aug 15, 2020, 2:42:38 PM8/15/20
to TiddlyWiki


On Saturday, August 15, 2020 at 11:36:52 AM UTC-7, Eric Shulman wrote:

The "else" filter only works if the preceding filter produces NO result... but if the {!!name} field exists, it has a value...  it's just blank text.


But that's not the puzzling situation. If name doesn't exist, then there is NO output. I hadn't even checked the case where name exists but is empty.

Eric Shulman

unread,
Aug 15, 2020, 2:49:27 PM8/15/20
to TiddlyWiki
On Saturday, August 15, 2020 at 11:42:38 AM UTC-7, Mark S. wrote:
On Saturday, August 15, 2020 at 11:36:52 AM UTC-7, Eric Shulman wrote:
The "else" filter only works if the preceding filter produces NO result... but if the {!!name} field exists, it has a value...  it's just blank text.

But that's not the puzzling situation. If name doesn't exist, then there is NO output. I hadn't even checked the case where name exists but is empty.

My *guess* is that {!!name} actually produces a blank value even if the field doesn't exist.

-e

Mark S.

unread,
Aug 15, 2020, 2:56:42 PM8/15/20
to TiddlyWiki
So, we can do :

[{!!name}!is[blank]else{!!title}]


This seems to work whether name exists or is merely empty.

Thanks!

amreus

unread,
Aug 15, 2020, 4:00:05 PM8/15/20
to tiddl...@googlegroups.com
Or

[{{!!name}is[blank]then{!!title}]]

which reads slightly better, imo.

Thanks for the discussion - I was next wanting to know how to show some info in a macro only if a field exists and is not blank. I think this will be useful.
Reply all
Reply to author
Forward
0 new messages