Introducing the standard tag library

At this point you're probably wondering how to add conditional logic. Outputting values to the page is interesting, but how do we do so conditionally? That is after all what makes dynamic web pages, well dynamic.

Adding the tag library

To add the tag library, we specify it's xml namespace and the class name. This can be done at any level of the HTML document before you attempt to use the tag, but it's simplest to add it to your <html> tag.

<html xmlns:c="urn:nsttl:HTML_Template_Nest_Taglib_Standard">

That says that I'll prefix all of my standard tag library tags with the letter 'c'. "urn:nsttl" says it's a local nest tag library that's in the include path, and the class is HTML_Template_Nest_Taglib_Standard.

Tag libraries follow PEAR naming conventions, so it will look for the file "HTML/Template/Nest/Taglib/Standard.php".

Once you've added that namespace declaration to the top of your file you can start using the standard tags.

<c:if>

The simplest conditional is the if tag. The if tag takes one attribute "test". The test attribute is a nest expression, but doesn't require the ${} token tags. Note that unlike JSP you don't wrap your conditional structures in a "choose" tag.
<c:if test="isAdmin">
   <a href="admin.html">Admin Interface<a>
</c:if>

<c:elseif>

The elseif works exactly like the if. It must be preceeded by an if tag, however. It also takes the test attribute.
<c:if test="isAdmin">
    <a href="admin.html">Admin Interface<a>
</c:if>
<c:elseif test="isPowerUser">
    <a href="power.html">Power Interface<a>
</c:elseif>

<c:else>

Else is the final part of a conditional. It must be preceeded by either an if tag or elseif tag. It does not have any attributes.
<c:if test="isAdmin">
    <a href="admin.html">Admin Interface<a>
</c:if>
<c:elseif test="isPowerUser">
    <a href="power.html">Power Interface<a>
</c:elseif>
<c:else>
    <a href="profile.html">User Profile<a>
</c:else>    

<c:foreach>

Foreach lets you iterate over an array. You can iterate over indexed arrays or associate arrays.

Indexed Arrays

Add an indexed array to your view:

$view->addAttribute("groceryList", Array("Bananas", "Milk", "Ground Beef"));

Your array is specified in the <foreach> tag using the items attribute. The value of var will be the local variable that wil contain the individual array element.

<h3>Shopping List</h3>
<ul>
    <c:foreach items="groceryList" var="shoppingListItem">
        <li>${shoppingListItem}</li>
    </c:foreach>
</ul>

Associative Arrays

Add an indexed array to your view:

$view->addAttribute("groceryReceipt", Array("Bananas" => ".75", "Milk" => "3.59", "Ground Beef" => "1.99"));

Your array and local variable for value are specified the same was an indexed array. Associate arrays add a key attribute for pulling out the array's key.

<h3>Shopping Receipt</h3>
<table>
    <tr>
        <th>Item</th>
        <th>Price</th>
    </tr>
    <c:foreach items="groceryReceipt" var="itemPrice" key="itemName">
        <tr>
            <td>${itemName}</td>
            <td>${itemPrice}</td>
        </tr>
    </c:foreach>
</table>

Row Count

Knowing the numeric index of the current item can be useful for many reasons. One common use is to alternate colors every other row. You can get that value by assigning a local variable name to the rowcount attribute. Let's alternate css classes in our previous example:
<h3>Shopping Receipt</h3>
<table>
    <tr>
        <th>Item</th>
        <th>Price</th>
    </tr>
    <c:foreach items="groceryReceipt" var="itemPrice" key="itemName" rowcount="i">
        <tr class="row${(i % 2 == 0 ? 'A' : 'B')}">
            <td>${itemName}</td>
            <td>${itemPrice}</td>
        </tr>
    </c:foreach>
</table>

This will alternate between the css class 'rowA' and 'rowB'

<c:for>

<c:for var="i" test="i < 10">
    Should print 10 times.
</c:for>
    Should print 10 times.
    Should print 10 times.
    Should print 10 times.
    Should print 10 times.
    Should print 10 times.
    Should print 10 times.
    Should print 10 times.	
    Should print 10 times.
    Should print 10 times.
    Should print 10 times.									

<c:attribute>

<div>
	<c:attribute name="class" trim="true">
	   	<c:for var="i" test="i < 10">
            class${i}
        </c:for>
	</c:attribute>
</div>
	<div class="class0 class1 class2 class3 class4 class5 class6 class7 class8 class9">
		
	</div>

<c:set>

<c:set var="mylocalvar" value="${bulletClass}">
    ${mylocalvar}
</c:set>