1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>hsc - Features - Conditionals</TITLE>
<LINK REV="owns" TITLE="Thomas Aglassinger" HREF="mailto:agi@giga.or.at">
<LINK REL="Next" HREF="prefs.html">
<LINK REL="Copyright" HREF="../copy.html">
<LINK REL="Previous" HREF="../macro/attrib.html">
<META name="ROBOTS" content="NOINDEX, NOFOLLOW">
</HEAD>
<BODY>
<A HREF="../index.html"><IMG SRC="../image/main.gif" ALT="Contents" ALIGN="middle" WIDTH="70" HEIGHT="16"></A>
<IMG SRC="../image/noindex.gif" ALT="-----" ALIGN="middle" WIDTH="70" HEIGHT="16">
<A HREF="../copy.html"><IMG SRC="../image/copy.gif" ALT="Copyright" ALIGN="middle" WIDTH="70" HEIGHT="16"></A>
<A HREF="../index.html"><IMG SRC="../image/back.gif" ALT="Up" ALIGN="middle" WIDTH="70" HEIGHT="16"></A>
<A HREF="../macro/attrib.html"><IMG SRC="../image/prev.gif" ALT="Previous" ALIGN="middle" WIDTH="70" HEIGHT="16"></A>
<A HREF="prefs.html"><IMG SRC="../image/next.gif" ALT="Next" ALIGN="middle" WIDTH="70" HEIGHT="16"></A>
<HR>
<H1>Conditionals</H1>
<H2><A NAME="general">General Syntax</A></H2>
Conditionals looks like that:
<PRE>
<CODE><$if COND=</CODE><I><A HREF="expressions.html">expression</A></I><CODE>></CODE>
<I>code to be processed if condition matches</I>
<CODE><$elseif COND=</CODE><I><A HREF="expressions.html">expression</A></I><CODE>></CODE>
<I>(optional) code to be processed if alternative condition matches;
this can occur multiple times</I>
<CODE><$else></CODE>
<I>(optional) code to be processed if none of the previous conditions matched</I>
<CODE></$if></CODE>
</PRE>
<P>Both <CODE><$if></CODE> and <CODE><$elseif></CODE> require a boolean attribute
<CODE>COND</CODE>; <CODE>false</CODE> is represented by an empty string,
<CODE>true</CODE> by any non-empty string. Normally, you will like to
set <CODE>COND</CODE> using
<A HREF="expressions.html">expressions</A>.</P>
<H2><A NAME="simple">Some Simple Examples</A></H2>
Now let's see how this works in practice:
<PRE>
<$if COND=(NAME="sepp")>
You must be sepp!
</$if>
</PRE>
<P>This one inserts the text "<CODE>You must be sepp!</CODE>", if the attribute
<CODE>NAME</CODE> has the value "<CODE>sepp</CODE>". Note that the
"<CODE>=</CODE>"-operator performs a case-insensitive string-comparison,
so setting <CODE>NAME="SEPP"</CODE> would lead to the same result.</P>
Now let's extend this:
<PRE>
<$if COND=(NAME="sepp")>
You must be sepp!
<$else>
I don't know you.
</$if>
</PRE>
<P>Setting <CODE>NAME="sepp"</CODE> will create the same text as above. Any
other value for <CODE>NAME</CODE> will insert
"<CODE>I don't know you.</CODE>".</P>
<H2><A NAME="nesting">Nesting Conditionals</A></H2>
<P>Nesting them is also possible, of course:</P>
<PRE>
<$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>
</PRE>
<H2><A NAME="macros">Conditionals And Macros</A></H2>
<P>You should not compare <KBD>hsc</KBD>'s <CODE><$if></CODE> with the primitive and
clumsy <CODE>#if</CODE> of the C-preprocessor. The main difference is
that you can use <CODE><$if></CODE> inside macros and that expressions are
recalculated for every new call of the macro.</P>
<PRE>
<$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>
</PRE>
<P>This macro inserts a link to an URI specified with <CODE>HREF</CODE>,
using <CODE>TITLE</CODE> 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.</P>
<P>The "<CODE>/r</CODE>" behind the declaration of the
macro-attributes is short for "<CODE>/required</CODE>" and means that
the macro needs both of these attributes to be set during the
macro-call.</P>
<P>For example, you can utilize this macro using</P>
<PRE>
You should read the document about recent
<TRY-A HREF="../bugfixes.html" TITLE="bufixes">
if there are any.
</PRE>
<P>This leads to the text</P>
<PRE>
You should read the document about recent bugfixes if there are any.
</PRE>
<P>with a anchor around the term "<CODE>bugfixes</CODE>" if the document
"<I>../bugfixes.html</I>" exists.</P>
</BODY></HTML>
|