<% if %> and match[] and <% else %> oddity

Some simple enough conditional code…
(and btw, <<alog...>> is merely a wrapper over <$action-log.../>)

    <% if [<dom-data-link>match[macro-doc-back-btn]] %>
      <<alog "go-back-btn" "dom-data-link top-var previous-var">>
      ...
    <% else %>
      <<alog "go-NOT-back-btn" "dom-data-link">>
      ...
    <% endif %>

In the console I get output from both log calls and in both cases dom-data-link=macro-doc-back-btn

After an hour of hair pulling, I tried this, which is crazy-stupid, but hey, it works:

    <% if [<dom-data-link>match[macro-doc-back-btn]] %>
      <<alog "go-back-btn" "dom-data-link top-var previous-var">>
      ...
    <% else %>
      <% if [<dom-data-link>!match[macro-doc-back-btn]] %>
        <<alog "go-NOT-back-btn" "dom-data-link">>
        ...
      <% endif %>
    <% endif %>

which kinda proves, not only is it hitting the else clause when it should not, but it then succeeds the next negated match: !match[] on the very thing it just failed on. IOW, the completely redundant second <% if %> makes the <% else %> work as it should have worked all along.

I’ve never encountered anything like this before and I have a ton of if-match/if-!match code that works just fine.

My brain is broken. :exploding_head:

Here is the actual code in full: it’s an eventcatcher action for a button element:

  \procedure go()
    <% if [<dom-data-link>match[macro-doc-back-btn]] %>
      <$let
        top-var=<<stack-pop visited-stack>>
        previous-var=<<stack-peek visited-stack>>
      >
      <<alog "go-back-btn" "dom-data-link top-var previous-var">>
      <$transclude $variable="stack-push" stackname="visited-stack" value=<<top-var>> />
      <$action-setfield $tiddler=<<data-tiddler>> $field=varname $value=<<previous-var>>/>
    <% else %>
      <<alog "go-NOT-back-btn" "dom-data-link">>
      <$action-setfield $tiddler=<<data-tiddler>> $field=varname $value=<<dom-data-link>>/>
      <<macro-doc-force-update>>
    <% endif %>
  \end go

The “fixed” version:

  \procedure go()
    <% if [<dom-data-link>match[macro-doc-back-btn]] %>
      <$let
        top-var=<<stack-pop visited-stack>>
        previous-var=<<stack-peek visited-stack>>
      >
      <<alog "go-back-btn" "dom-data-link top-var previous-var">>
      <$transclude $variable="stack-push" stackname="visited-stack" value=<<top-var>> />
      <$action-setfield $tiddler=<<data-tiddler>> $field=varname $value=<<previous-var>>/>
    <% else %>
      <% if [<dom-data-link>!match[macro-doc-back-btn]] %>
      <<alog "go-NOT-back-btn" "dom-data-link">>
      <$action-setfield $tiddler=<<data-tiddler>> $field=varname $value=<<dom-data-link>>/>
      <<macro-doc-force-update>>
      <% endif %>
    <% endif %>
  \end go

The button:

    <$eventcatcher tag=span selector="button.doc-link-btn" $click=<<go>> stopPropagation=always>
      <button class="tc-btn-invisible doc-link-btn" data-link=<<doc-link-name>>><code><<doc-link-name>></code></button>
    </$eventcatcher>

I’m not sure if this is the cause, but I notice that you have an unmatched <$let> within the <% if %>
Try adding a </$let> just before the <% else %>

-e

Ever seen a grown man cry? How about tears of blood? :laughing:

Decorum, politeness and etiquet prevent me from saying (typing) what I want to say :wink:

Thank you, Eric.

1 Like
  1. VScode have syntax highlighting, so it will be easier to find mismatching widget [Howto] Use TW5-Syntax plugin for VSCode.dev
  2. The core just recommend not using whitespace between % and if like <%if [[]] %>! Standardise whitespace in conditional shortcut syntax by Jermolene · Pull Request #8583 · TiddlyWiki/TiddlyWiki5 · GitHub