You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@cocoon.apache.org by da...@cocoon.zones.apache.org on 2005/10/14 15:28:37 UTC

[DAISY] Updated: Advanced Control Flow - JXTemplate

A document has been updated:

http://cocoon.zones.apache.org/daisy/legacydocs/511.html

Document ID: 511
Branch: main
Language: default
Name: Advanced Control Flow - JXTemplate (previously Advanced Control Flow)
Document Type: Document (unchanged)
Updated on: 10/14/05 1:28:21 PM
Updated by: Helma van der Linden

A new version has been created, state: publish

Parts
=====
Content
-------
This part has been updated.
Mime type: text/xml (unchanged)
File name:  (unchanged)
Size: 22304 bytes (previous version: 22345 bytes)
Content diff:
(20 equal lines skipped)
    <tt>org.apache.cocoon.generation.JXTemplateGenerator</tt>, for example like
    this:</p>
    
--- <pre>
--- &lt;map:generators&gt;
+++ <pre>&lt;map:generators&gt;
      &lt;map:generator label="content,data" 
          logger="sitemap.generator.jx" name="jx" 
             src="org.apache.cocoon.generation.JXTemplateGenerator"/&gt;
(17 equal lines skipped)
    <p>Having an embedded expression language allows a page author to access an
    object using a simple syntax such as</p>
    
--- <pre>
--- &lt;site signOn="${accountForm.signOn}"&gt;
---  </pre>
+++ <pre>&lt;site signOn="${accountForm.signOn}"&gt;
+++ </pre>
    
    <p>Embedded Jexl expressions are contained in <tt>${}</tt>.</p>
    
(9 equal lines skipped)
    <ul>
    <li><a href="daisy:518#request">request</a>: <br/>
    The current Cocoon request</li>
--- <li><br/>
--- <a href="daisy:518#session">session</a>: <br/>
+++ <li><a href="daisy:518#session">session</a>: <br/>
    The user session associated with the current request</li>
--- <li><br/>
--- <a href="daisy:518#context">context</a>: <br/>
+++ <li><a href="daisy:518#context">context</a>: <br/>
    The Cocoon context associated with the current request</li>
--- <li><br/>
--- <tt>parameters</tt>: <br/>
+++ <li><tt>parameters</tt>: <br/>
    A map containing the parameters passed to the generator in the pipeline</li>
--- <li><br/>
--- <a href="daisy:518#WebContinuation">continuation</a>: <br/>
+++ <li><a href="daisy:518#WebContinuation">continuation</a>: <br/>
    The current Web Continuation from your Flowscript</li>
    </ul>
    
    <p>Jexl Example:</p>
    
--- <pre>
--- The content type of the current request is ${cocoon.request.contentType}
+++ <pre>The content type of the current request is ${cocoon.request.contentType}
    </pre>
    
    <p>JXPath Example:</p>
    
--- <pre>
--- The content type of the current request is #{$cocoon/request/contentType}
+++ <pre>The content type of the current request is #{$cocoon/request/contentType}
    </pre>
    
    <p>You would typically access the <tt>id</tt> of the Web Continuation:</p>
    
--- <pre>
--- &lt;form action="${cocoon.continuation.id}"&gt;
+++ <pre>&lt;form action="${cocoon.continuation.id}"&gt;
    </pre>
    
    <p>You can also reach previous continuations via its <tt>parent</tt> property:
    </p>
    
--- <pre>
--- &lt;form action="${cocoon.continuation.parent.id}" &gt;
+++ <pre>&lt;form action="${cocoon.continuation.parent.id}" &gt;
    </pre>
    
    <p>or using an XPath expression:</p>
    
--- <pre>
--- &lt;form action="#{$cocoon/continuation/parent/id}" &gt;
+++ <pre>&lt;form action="#{$cocoon/continuation/parent/id}" &gt;
    </pre>
    
    <p>Deprecated Variables:</p>
(32 equal lines skipped)
    
    <p>Example:</p>
    
--- <pre>
--- 
---     &lt;map:match pattern="*.jx"&gt;
+++ <pre>    &lt;map:match pattern="*.jx"&gt;
          &lt;map:generate type="jx" src="documents/{1}.jx"&gt;
               &lt;map:parameter name="lenient-xpath" value="true"/&gt;
          &lt;/map:generate&gt;
          &lt;map:serialize type="xhtml"/&gt;
        &lt;/map:match&gt;
--- 
    </pre>
    
    <h1>Tags</h1>
    
    <p>The JXTemplate Generator tags are defined in the namespace</p>
    
--- <pre>
--- http://apache.org/cocoon/templates/jx/1.0
+++ <pre>http://apache.org/cocoon/templates/jx/1.0
    </pre>
    
    <h2>template</h2>
    
    <p>The <tt>template</tt> tag defines a new template:</p>
    
--- <pre>
--- &lt;jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"&gt;
+++ <pre>&lt;jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"&gt;
       body
    &lt;/jx:template&gt;
    </pre>
(4 equal lines skipped)
    current template. The content of the imported template is compiled and will be
    executed in place of the <tt>import</tt> tag:</p>
    
--- <pre>
--- &lt;jx:import uri="URI" [context="Expression"]/&gt;
+++ <pre>&lt;jx:import uri="URI" [context="Expression"]/&gt;
    </pre>
    
    <p>The Cocoon source resolver is used to resolve <tt>uri</tt>. If
(7 equal lines skipped)
    <tt>value</tt> attribute specifies the object (defaults to <tt>body</tt> if not
    present):</p>
    
--- <pre>
--- &lt;jx:set var="Name" [value="Value"]&gt;
+++ <pre>&lt;jx:set var="Name" [value="Value"]&gt;
       [body]
    &lt;/jx:set&gt;
    </pre>
(3 equal lines skipped)
    
    <p>Jexl Example:</p>
    
--- <pre>
--- &lt;jx:set var="greeting" value="Hello ${user}"/&gt;
+++ <pre>&lt;jx:set var="greeting" value="Hello ${user}"/&gt;
    The value of greeting is ${greeting}
    </pre>
    
    <p>JXPath Example:</p>
    
--- <pre>
--- &lt;jx:set var="greeting" value="Hello #{user}"/&gt;
+++ <pre>&lt;jx:set var="greeting" value="Hello #{user}"/&gt;
    The value of greeting is #{$greeting}
    </pre>
    
(2 equal lines skipped)
    <p>The <tt>if</tt> tag allows the conditional execution of its body according to
    value of its <tt>test</tt> attribute:</p>
    
--- <pre>
--- &lt;jx:if test="Expression"&gt;
+++ <pre>&lt;jx:if test="Expression"&gt;
      body
    &lt;/jx:if&gt;
    </pre>
    
    <p>Jexl Example:</p>
    
--- <pre>
--- &lt;jx:if test="${cart.numberOfItems == 0}"&gt;
+++ <pre>&lt;jx:if test="${cart.numberOfItems == 0}"&gt;
      Your cart is empty
    &lt;/jx:if&gt;
    </pre>
    
    <p>JXPath Example:</p>
    
--- <pre>
--- &lt;jx:if test="#{cart/numberOfItems = 0}"&gt;
+++ <pre>&lt;jx:if test="#{cart/numberOfItems = 0}"&gt;
      Your cart is empty
    &lt;/jx:if&gt;
    </pre>
(6 equal lines skipped)
    conditions of its nested <tt>when</tt> tags evaluate to <tt>true</tt>, then the
    body of its <tt>otherwise</tt> tag is evaluated, if present:</p>
    
--- <pre>
--- &lt;jx:choose&gt;
+++ <pre>&lt;jx:choose&gt;
      &lt;jx:when test="Expression"&gt;
        body
      &lt;/jx:when&gt;+
(5 equal lines skipped)
    
    <p>Jexl Example:</p>
    
--- <pre>
--- &lt;jx:choose&gt;
+++ <pre>&lt;jx:choose&gt;
      &lt;jx:when test="${!user.loggedIn}"&gt;
        &lt;jx:set var="label" value="Log in"&gt;
      &lt;/jx:when&gt;
(5 equal lines skipped)
    
    <p>JXPath Example:</p>
    
--- <pre>
--- &lt;jx:choose&gt;
+++ <pre>&lt;jx:choose&gt;
      &lt;jx:when test="#{not(user/loggedIn)}"&gt;
        &lt;jx:set var="label" value="Log in"&gt;
      &lt;/jx:when&gt;
(8 equal lines skipped)
    <p>The <tt>out</tt> tag evaluates an expression and outputs the result of the
    evaluation:</p>
    
--- <pre>
--- &lt;jx:out value="Expression"/&gt;
+++ <pre>&lt;jx:out value="Expression"/&gt;
    </pre>
    
    <p>Jexl Example:</p>
    
--- <pre>
--- &lt;jx:out value="${cart.numberOfItems}"&gt;
+++ <pre>&lt;jx:out value="${cart.numberOfItems}"&gt;
    </pre>
    
    <p>JXPath Example:</p>
    
--- <pre>
--- &lt;jx:out value="#{cart/numberOfItems}"&gt;
+++ <pre>&lt;jx:out value="#{cart/numberOfItems}"&gt;
    </pre>
    
    <h2>forEach</h2>
    
    <p>The <tt>forEach</tt> tag allows you to iterate over a collection of objects:
    </p>
    
--- <pre>
--- &lt;jx:forEach [var="Name"] [varStatus="Name"] [items="Expression"] 
+++ <pre>&lt;jx:forEach [var="Name"] [varStatus="Name"] [items="Expression"] 
                   [begin="NumExpr"] [end="NumExpr"] [step="NumExpr"]&gt;
      body
    &lt;/jx:forEach&gt;
(13 equal lines skipped)
    XPath (since you can specify the selection criteria for the collection using
    XPath itself):</p>
    
--- <pre>
--- &lt;jx:forEach select="XPathExpression"&gt;
+++ <pre>&lt;jx:forEach select="XPathExpression"&gt;
      body
    &lt;/jx:forEach&gt;
    </pre>
(3 equal lines skipped)
    
    <p>Jexl Example:</p>
    
--- <pre>
--- &lt;jx:forEach var="item" items="${cart.cartItems}" 
+++ <pre>&lt;jx:forEach var="item" items="${cart.cartItems}" 
                   begin="${start}" end="${count-start}" step="1"&gt;
       &lt;td&gt;${item.productId}&lt;/td&gt;
    &lt;/jx:forEach&gt;
    </pre>
    
    <p>JXPath Example:</p>
    
--- <pre>
--- &lt;jx:forEach select="#{cart/cartItems[position() &amp;lt;= $count]}}&gt;
+++ <pre>&lt;jx:forEach select="#{cart/cartItems[position() &amp;lt;= $count]}}&gt;
       &lt;td&gt;#{./productId}&lt;/td&gt;
    &lt;/jx:forEach&gt;
    </pre>
(4 equal lines skipped)
    following properties:</p>
    
    <table>
+++ <tbody>
    <tr>
--- <td>
+++ <th>
    <p>Property:</p>
--- </td>
--- <td>
+++ </th>
+++ <th>
    <p>Description:</p>
--- </td>
+++ </th>
    </tr>
    <tr>
    <td>
(59 equal lines skipped)
    <p>The value of the <tt>step</tt> attribute</p>
    </td>
    </tr>
+++ </tbody>
    </table>
    
    <p>Jexl Example:</p>
    
--- <pre>
--- &lt;jx:forEach items="${items}" varStatus="status"&gt;
+++ <pre>&lt;jx:forEach items="${items}" varStatus="status"&gt;
        index=${status.index},
        count=${status.count},
        current=${status.current},
(6 equal lines skipped)
    
    <p>JXPath Example:</p>
    
--- <pre>
--- &lt;jx:forEach select="#{items}" varStatus="status"&gt;
+++ <pre>&lt;jx:forEach select="#{items}" varStatus="status"&gt;
        index=#{$status/index},
        count=#{$status/count},
        current=#{$status/current},
(11 equal lines skipped)
    locale, for example, whether to use a period or a comma for delimiting the
    integer and decimal portions of a number. Here is its syntax:</p>
    
--- <pre>
--- &lt;jx:formatNumber value="Expression"
+++ <pre>&lt;jx:formatNumber value="Expression"
        [type="Type"] [pattern="Expression"]
        [currencyCode="Expression"] [currencySymbol="Expression"]
        [maxIntegerDigits="Expression"] [minIntegerDigits="Expression"]
(41 equal lines skipped)
    
    <p>The <tt>formatDate</tt> tag provides facilities to format Date values:</p>
    
--- <pre>
--- &lt;jx:formatDate value="Expression" [dateStyle="Style"] 
+++ <pre>&lt;jx:formatDate value="Expression" [dateStyle="Style"] 
      [timeStyle="Style"] [pattern="Expression"] [type="Type"] [var="Name"] 
      [locale="Expression"]&gt;
    </pre>
(38 equal lines skipped)
    
    <p>The <tt>macro</tt> tag allows you define a new custom tag.</p>
    
--- <pre>
--- &lt;jx:macro name="Name" [targetNamespace="Namespace"]&gt;
+++ <pre>&lt;jx:macro name="Name" [targetNamespace="Namespace"]&gt;
      &lt;jx:parameter name="Name" [optional="Boolean"] [default="Value"]/&gt;*
      body
    &lt;/jx:macro&gt;
    </pre>
    
    <p>For example:</p>
    
--- <pre>
--- &lt;jx:macro name="d"&gt;
+++ <pre>&lt;jx:macro name="d"&gt;
      &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;/jx:macro&gt;
    </pre>
    
    <p>The tag being defined in this example is <tt>&lt;d&gt;</tt> and it can be
    used like any other tag:</p>
    
--- <pre>
--- &lt;d/&gt;
+++ <pre>&lt;d/&gt;
    </pre>
    
    <p>However, when this tag is used it will be replaced with a row containing a
(3 equal lines skipped)
    available as variables in the body of the <tt>macro</tt>'s definition, for
    example:</p>
    
--- <pre>
--- &lt;jx:macro name="tablerows"&gt;
+++ <pre>&lt;jx:macro name="tablerows"&gt;
      &lt;jx:parameter name="list"/&gt;
      &lt;jx:parameter name="color"/&gt;
      &lt;jx:forEach var="item" items="${list}"&gt;
(13 equal lines skipped)
    
    <p>and a template like this:</p>
    
--- <pre>
--- &lt;table&gt;
+++ <pre>&lt;table&gt;
       &lt;tablerows list="${greatlakes}" color="blue"/&gt;
    &lt;/table&gt;
    </pre>
    
    <p>When the <tt>tablerows</tt> tag is used in this situation the following
    output would be generated:</p>
    
--- <pre>
--- &lt;table&gt;
+++ <pre>&lt;table&gt;
      &lt;tr&gt;&lt;td bgcolor="blue"&gt;Superior&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td bgcolor="blue"&gt;Michigan&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td bgcolor="blue"&gt;Huron&lt;/td&gt;&lt;/tr&gt;
(10 equal lines skipped)
    <a href="http://java.sun.com/products/jsp/jstl/">JSTL</a> <tt>forTokens</tt>
    tag:</p>
    
--- <pre>
--- 
--- &lt;jx:macro name="forTokens"&gt;
+++ <pre>&lt;jx:macro name="forTokens"&gt;
      &lt;jx:parameter name="var"/&gt;
      &lt;jx:parameter name="items"/&gt;
      &lt;jx:parameter name="delims"/&gt;
(6 equal lines skipped)
    
    <p>The tag produced by this macro can be used like this:</p>
    
--- <pre>
--- 
--- &lt;forTokens var="letter" items="a,b,c,d,e,f,g" delims=","&gt;
+++ <pre>&lt;forTokens var="letter" items="a,b,c,d,e,f,g" delims=","&gt;
      letter = ${letter} &lt;br/&gt;
    &lt;/forTokens&gt;
     </pre>
    
    <p>which would create the following output:</p>
    
--- <pre>
--- 
---   letter = a  &lt;br/&gt;  
+++ <pre>  letter = a  &lt;br/&gt;  
      letter = b  &lt;br/&gt;  
      letter = c  &lt;br/&gt;  
      letter = d  &lt;br/&gt;  
(6 equal lines skipped)
    
    <p>The <tt>eval</tt> tag permits dynamic evaluation of custom tags.</p>
    
--- <pre>
--- &lt;jx:eval select="Expression"/&gt;
+++ <pre>&lt;jx:eval select="Expression"/&gt;
    </pre>
    
    <p>Within the body of a macro, information about the current invocation is
    available via a special variable <tt>macro</tt>. This variable contains the
    following properties:</p>
    
    <table>
+++ <tbody>
    <tr>
--- <td>
+++ <th>
    <p>Property:</p>
--- </td>
--- <td>
+++ </th>
+++ <th>
    <p>Description:</p>
--- </td>
+++ </th>
    </tr>
    <tr>
    <td>
(11 equal lines skipped)
    <p>A reference to the content of the tag invocation</p>
    </td>
    </tr>
+++ </tbody>
    </table>
    
    <p>You can store the value of <tt>body</tt> in another variable and invoke it
(2 equal lines skipped)
    </p>
    
    <pre>
--- 
--- 
    &lt;jx:set var="tags" value="${java.util.HashMap()}"/&gt;
    
    &lt;jx:macro name="dynamic-tag"&gt;
(14 equal lines skipped)
    
    <p>The above template produces the following output:</p>
    
--- <pre>
+++ <pre>&lt;p&gt;I'm about to invoke a dynamic tag:&lt;/p&gt;
    
--- &lt;p&gt;I'm about to invoke a dynamic tag:&lt;/p&gt;
--- 
      
    &lt;em&gt;This tag was invoked dynamically&lt;/em&gt;
     
(4 equal lines skipped)


Fields
======
no changes

Links
=====
no changes

Custom Fields
=============
no changes

Collections
===========
no changes