PageTemplate - If, Else, and Elsif
if directive tells PageTemplate to only display a chunk of content
when some condition is true. PageTemplate will skip the block and move
on if the condition is false.
Here is an example of
if in use.
In this example, if the application tells PageTemplate that
is true, PageTemplate inserts a link to an administrative page.
Otherwise, nothing happens here.
else directive adds extra power to
if, by indicating a chunk of
content to use when a condition is not true.
This is the situation where I use
else directives the most. If the
visitor is logged in to a Web application, she is shown a brief
welcoming message. If not, then she will see a login form.
This example also shows a convenient approach to
if conditions. We
could make up a special
logged_in variable, but since all we care
about here is the presence of a login, we have PageTemplate test that as
if it were a regular condition.
There are many cases where a simple “yes” or “no” doesn’t do enough. You
want to display something different in the same spot depending on
whether or not different conditions are true. PageTemplate makes that a
little easier with the
elsif directive, which allows you to do exactly
that by testing the truth of different variables.
Sometimes you want to ask if something is false. For example, you may
want to show a login form in one spot if the user is not logged in, but
nothing at all if he is logged in. That is exactly the sort of thing
unless was intended for.