Content ViewSourceSitemap

Apache Cocoon 2.1.9 allows to use {#expr} to replace the value of the Java expression expr within attribute values and text nodes.

For expression interpolation in text nodes the difference is to the usual xsp:expr syntax is minor. For dynamic attribute values, however, interpolation keeps it much more concise and readable.

        
        <xsp:logic>
          String world = "world";
          String color = "red";
        </xsp:logic>
        
      

Just compare this:

        <center style="color:{#color}">Hello simple {#world}!</center>
      
Hello simple world!

to that:

        <center><xsp:attribute name="style">color:<xsp:expr>color</xsp:expr></xsp:attribute>Hello complex <xsp:expr>world</xsp:expr>!</center>
      
Hello complex world!

Interpolation can also be used in logicsheets:

Hello logicsheet world!

The XSP expression parser understands nested braces, single and double quoted strings, and backslash-escaped quotes and apostrophes.

Expression Result Expected
{#new String[]{}.length} 0 0
{#new String[]{"}"}.length} 1 1
{#"'\"}".indexOf('}')} 2 2