Skip to content

Conditions

Suppenhuhn79 edited this page May 17, 2021 · 7 revisions

Parts of a snippet can be produced by condition. Condition elements can be used anywhere within a snippet.

There are two kinds of condition statements:

a) Simple condition

<ps:if test="'{{foo}}' === 'bar'">
  <!-- elements if condition holds true -->
</ps:if>

The content is produced, if the test holds true.

b) Complex choice (if-then-else/select)

<ps:choose mode="strict">
  <ps:if test="'{{foo}}' === 'bar'">
    <!-- elements -->
  </ps:if>
  <ps:else>
    <!-- elements -->
  </ps:else>
</ps:choose>

A <ps:choose> may contain numerous <ps:if>-tests and one <ps:else> that will be produced if none of the ifs holds true.

There are two modes of processing the choice:

  • mode="strict" (default) only the first matching <ps:if> will be produced, then the choice ends
  • mode="lax" all matching <ps:if> will be produced.

test-expressions

A test-expression will be evaluated as a string. Meaning, you can only access any of placeholders properties, but not execute methods.

Example:

<!-- this will not work -->
<ps:if test="typeof {{myObject}} === 'object'">

Provide a placeholder typeof_myObject = typeof myObject (which provides an evaluable string) instead or test on '{{myObject.constructor.name}}'.

Keep in mind to enclose string values in quotes.

Clone this wiki locally