Conditional Shortcut Syntax in functions?

Short question: is it permitted to use the Conditional Shortcut Syntax within a function definition?

Edit: I misunderstood the question to ask for if conditional syntax was permitted in macros/procedures, but you ask for functions! Then I have no useful reply, see next post by @etardiff instead :slight_smile:

Below is my previous reply:


“Let me google that for you!” :wink: :wink:

try this

\define foo()
<% if [{$:/info/url/protocol}match[file:]] %>
  Loaded from a file URI
<% else %>
  Not loaded from a file URI
<% endif %>
\end

<<foo>>

Short answer: No. You can use the conditional syntax in a macro or procedure definition, as Mat illustrated, but functions only use filter syntax. It’s best to think of them as a more flexible alternative to <$let variable={{{ filter }}}>. (Here’s a more in-depth comparison, if you’re interested.)

However, you can generally achieve similar results with a function using filter expressions. For instance, I could rewrite Mat’s example as follows:

\function foo.function()
[{$:/info/url/protocol}match[file:]]
:then[[Loaded from a file URI]]
:else[[Not loaded from a file URI]]
\end

<<foo.function>>

For more complicated cases, there’s also the :cascade mechanism.

2 Likes

Thanks. I wanted to write a recursive function, so a simple if-then-else syntax would come in handy to handle the bottom of the recursion. There is more to it than I thought. So now I am trying to get my head around the documentation, but it is quite a rabbit hole.

1 Like

Wanted, but I can’t successful make it