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.
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 test="isAdmin"> <a href="admin.html">Admin Interface<a> </c:if>
<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: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>
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>
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>
<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 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.
<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 var="mylocalvar" value="${bulletClass}"> ${mylocalvar} </c:set>