Action code in templates emitting itself

Quite likely, I’ve got something foundational wrong here.

I have the following as part of an export template:

^indent(#)^ ^sectionNumber(this)^ ^value($TrimmedName)^

^var txt:string^ ^action(if($Text.contains("---")) {txt=($Text.split("---")[0]);} else {txt=$Text;})^^value(txt)^

^descendants(/Templates/Markdown/MarkdownSection)^

In many of my notes, I use the triple (-or-more) dash convention to separate the actual text from cruft I’m not sure what to do with. I’d hoped I’d created a bit of action code that would take the text before the the ‘—’ and use that as the note text (stored temporarily as the variable txt) - however, the rendered output file collapses it into

^ var txt:string=;txt

instead.

Is this something I should be doing as an agent that prepares the text in advance (there’s a lot of text already, and it seems that running this as an ad-hoc background agent may lead to a less smooth working experience than to generate it only when needed, at export time)?

Syntax error! Export code has no var() operator. Also the syntax use for delacring the vaiable is worng. You need to use (line breaks in code for clarity):

^action(
var:string txt;
if($Text.contains("---")) {
   txt=($Text.split("---")[0]);
} else {
   txt=$Text;
})^
^value(txt)^

Testing in v10.0.2, replacing ^text in the body of the ‘HTML Single Note’ template with a test note $Text of:

Hello
---
World

The HTML body content emitted is:

Untitled 2025-01-30 10-56-15

Notes:

  • ^value(txt)^ is not processed as for ^text^ to add paragraph mark-up so it is up to the user to insert enclosing <p></p> tags when (if!) exporting to HTML.
  • If using this 3 hyphen method in default note $Text you may want to turn off ‘smart quotes’ else the OS/app will interpret typed input of 3 successive hyphens as an em-dash, i.e. ‘---’ is auto-replaced with ‘—’. See $SmartQuotes.
3 Likes

My god, that fixed it up. Thank you, @mwra

While I was feverishly hunting around documentation, I read that template export code had to be collapsed onto a single line; I guess (and am thankful for the fact) that no longer seems to be the case.

The similarity to JavaScript constantly throws me. Those datatype declarations always feel like they’re happening to the wrong place, for me.

1 Like

Hmm. I think I know the possible cause of that. Line breaks inside export ^-delimited code are not exported. Line breaks outside such code form part of the (HTML/plaintext) export. The fun comes when one tries to make easility readable export code and emit ‘pretty’ HTML. At some point things may look ugly, at least to start with.

A simple example:

</p>
^($SomeValue)^<p>Hello!</p>^endif^
<p>

emits an empty line to HTML if $SomeValue evaluates as empty/nothing. Otherwise it neatly inserts an extra HTML paragraph on its only line of source code text. But:

</p>
^($SomeValue)^<p>Hello!</p>
^endif^<p>

does the same except there is no blank line in the HTML code if $SomeValue is false.

Essentially there is a trade, easy to read template vs pretty HTML code. For most folk, especially HTML novices, I’d suggest making the template understandable to oneself. Once the HTML works one can, if desired, go and ‘uglify’ the template so the HTML look neater. Folk used to coding/scripting and who know HTML can likely skip to templates where the HTML looks neat (if you care about that).

Important: if the ‘documentation’ you recall is still around, i.e. in Help or aTbRef or even a post in the current forum, it would be useful to know where it is, as that source warrants review and likely correction/clarification to avoid precisely the confusion you report.

I do get it: things aren’t obvious until they are due to unstated assumptions on the part of the writer about the readers knowledge/expertise. Much of the growth of some aTbref articles has been about surfacing these assumptions—and i’d not be so naïve as to assume I’ve got it totally right. The flip side of the problem is it is hard, once knowledgeable, to know what one didn’t know at outset.for aTbRef I’m forever thankfully to those who flag those gaps in my articles, so we can inch towards ‘better’

As above, I get that. I’m constantly dipping in an out of different scripts/languages none of which I’m grounded in and they never make sense to me … as I’m not experienced in them. I’m not sure there is an easy fix.

Action code isn’t aligned to any one language. It is actually an evolved scripting system and has borrowed ideas over its 20+ year evolution. So looks-like ! = same. :open_mouth:

1 Like