DotAdmin User Guide
Conditional Instructions
Sometimes, it is handy to be able to change the output from a template depending on whether the content you are formatting exists or not. dotAdmin handles this situation using IF and ELSE statements.
Continuing our example from the previous section; displaying a list of links within a table: We have already established that if there are no links to display, the contents of BLOCK {pagelist} will never be output. This creates a problem because the opening and closing <table> tags will still appear, because they are not within the BLOCK.
To avoid this situation, we can make the <table> tags appear only if there is data to display in {pagelist}, using an IF instruction:
<!--IF {pagelist} -->
<table>
<!--BLOCK {pagelist} -->
<tr>
<td>
<a href="/content/{id}">{title}</a>
</td>
</tr>
<!-- /BLOCK {pagelist} -->
</table>
<!-- /IF {pagelist} -->
We can also provide alternative content in cases where the value is missing, by using an ELSE instruction following the IF instruction. See the example below:
<a href="/{id}">
<!-- IF {thumbnail_image} -->
<img src="{thumbnail_image}">
<!-- /IF {thumbnail_image} -->
<!-- ELSE -->
{title}
<!-- /ELSE -->
</a>
In the above example, an image is used as a link for any page that has a {thumbnail_image} assigned to it. If a page does not have a thumbnail image, the page {title} is used as link text instead.
Unlike the IF instruction, the ELSE instruction does not contain the name of a variable. The template system always treats an ELSE instruction as being the partner of the last IF instruction it found.
Nested Conditional Instructions
IF and ELSE instructions cannot contain other IF and ELSE instructions, except where the inner instructions are within a different BLOCK. For example, the following code is valid:
<!--IF {pagelist} -->
<table>
<!--BLOCK {pagelist} -->
<a href="/content/{id}">
<!-- IF {thumbnail_image} -->
<img src="{thumbnail_image}">
<!-- /IF {thumbnail_image} -->
<!-- ELSE -->
{title}
<!-- /ELSE -->
</a>
<!-- /BLOCK {pagelist} -->
</table>
<!-- /IF {pagelist} -->
The above example is valid because the inner IF and ELSE instructions are contained within BLOCK {pagelist}.
The example below is not valid, because the inner IF instructions are not contained within a BLOCK:
<a href="/content/{id}">
<!-- IF {thumbnail_image} -->
<img src="{thumbnail_image}">
<!-- /IF {thumbnail_image} -->
<!-- ELSE -->
{title}
<!-- IF {short_description} -->
<p>{short_description}</p>
<!-- /IF {short_description} -->
<!-- /ELSE -->
</a>
The explanation for this seemingly-odd behaviour is actually quite straightforward: The system cannot understand conditional instructions within other conditional instructions, but, all BLOCKs are treated as mini-templates by the system and are processed independently. So, in effect, when the system is dealing with the BLOCK part of the template, it cannot "see" the outer conditional instructions if they are not contained within that BLOCK.
|