# Conditionals

## General Syntax

Conditionals looks like that:

    <$if COND=expression>

      code to be processed if condition matches

    <$elseif COND=expression>

      (optional) code to be processed if alternative condition matches;
         this can occur multiple times

    <$else>

      (optional) code to be processed if none of the previous conditions matched

    </$if>

Both `<$if>` and `<$elseif>` require a boolean attribute `COND`; `false` is represented by an empty string, `true` by any non-empty string. Normally, you will like to set `COND` using expressions.

## Some Simple Examples

Now let's see how this works in practice:

        <$if COND=(NAME="sepp")>
           You must be sepp!
        </$if>

This one inserts the text "`You must be sepp!`", if the attribute `NAME` has the value "`sepp`". Note that the "`=`"-operator performs a case-insensitive string-comparison, so setting `NAME="SEPP"` would lead to the same result.

Now let's extend this:

        <$if COND=(NAME="sepp")>
           You must be sepp!
        <$else>
           I don't know you.
        </$if>

Setting `NAME="sepp"` will create the same text as above. Any other value for `NAME` will insert "`I don't know you.`".

## Nesting Conditionals

Nesting them is also possible, of course:

        <$if COND=(NAME="sepp")>
           You must be sepp!
           <$if COND=(HOME="austria")>
               Hollareiduliö! An Austrian!
           <$else>
               <(HOME)>? Never been there.
           </$if>
        <$else>
           A strange guy from a strange place.
        </$if>

## Conditionals And Macros

You should not compare `hsc`'s `<$if>` with the primitive and clumsy `#if` of the C-preprocessor. The main difference is that you can use `<$if>` inside macros and that expressions are recalculated for every new call of the macro.

        <$macro TRY-A HREF:uri/r TITLE:string/r>
        <$if COND=(Exists(HREF))>
            <a HREF=(HREF)><(TITLE)></a> <* insert link to HREF *>
        <$else>
            <(TITLE)>                    <* insert plain title *>
        </$if>
        </$macro>

This macro inserts a link to an URI specified with `HREF`, using `TITLE` as link text; but only, if the destination (local) URI can be accessed. If the required document is not available, only the plain text without a link will be inserted.

The "`/r`" behind the declaration of the macro-attributes is short for "`/required`" and means that the macro needs both of these attributes to be set during the macro-call.

For example, you can utilize this macro using

        You should read the document about recent
        <try-a HREF="../bugfixes.html" TITLE="bufixes">
        if there are any.

This leads to the text

        You should read the document about recent bugfixes if there are any.

with a anchor around the term "`bugfixes`" if the document "../bugfixes.html" exists.