This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/

Conditional Shortcut Syntax

 1st September 2023 at 12:31pm

New in v5.3.2 The conditional shortcut syntax provides a convenient way to express if-then-else logic within WikiText. It evaluates a filter expression and considers the condition to be true if there is at least one result (regardless of the value of that result).

Within an "if" or "elseif" clause, the variable condition contains the value of the first result of evaluating the filter condition.

A simple example:

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

That renders as:

Not loaded from a file URI

One or more <% elseif %> clauses may be included before the <% else %> clause:

<% if [{$:/info/url/protocol}match[file:]] %>
  Loaded from a file URI
<% elseif [{$:/info/url/protocol}match[https:]] %>
  Loaded from an HTTPS URI
<% elseif [{$:/info/url/protocol}match[http:]] %>
  Loaded from an HTTP URI
<% else %>
  Loaded from an unknown protocol
<% endif %>

That renders as:

Loaded from an unknown protocol

The conditional shortcut syntax can be nested:

\procedure test(animal)
<% if [<animal>match[Elephant]] %>
  It is an elephant
<% else %>
  <% if [<animal>match[Giraffe]] %>
    It is a giraffe
  <% else %>
    It is completely unknown
  <% endif %>
<% endif %>
\end

<<test "Giraffe">>

<<test "Elephant">>

<<test "Antelope">>

That renders as:

It is a giraffe It is an elephant It is completely unknown

Notes:

  • Clauses are parsed in inline mode by default. Force block mode parsing by following the opening <% if %>, <% elseif %> or <% else %> with two line breaks
  • Widgets and HTML elements must be within a single conditional clause; it is not possible to start an element in one conditional clause and end it in another
  • The conditional shortcut syntax cannot contain pragmas such as procedure definitions