[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]
Subject: RE: [tamie] The case for CTL Functions
There has been some question about whether we need functions written in CTL, since scriptlets can also produce effects. I think they are still desirable. Here's why.
Suppose you have a complicated business rule. I'll illustrate with one that determines the discount an order is elligible for. The logic can be placed in a scriptlet.
<scriptlet name="get-discount">
<param name="order">
<decide>
<if expr="$order/type='TypeA'">
<if expr="$order/total
> 1000">15%</if>
<if expr="$order/total >
100">2%</if>
</if>
<elseif
expr="$order/type='TypeB'">
<if expr="$order/total >
500">5%</if>
<if expr="$order/total >
200">1%</if>
</elseif>
<else>0%</else>
</decide>
</scriptlet>
If you need to call this logic from inside another scriptlet, it's relatively straightforward.
<var name="discount">
<start scriptlet="get-discount">
<with-param name="order"
expr="$order"/>
</start>
</var>
If the logic were placed in a function instead, it could be expressed more compactly.
<var name="discount" expr="get-discount($order)"/>
But the real benefit is that it allows you to use this logic inside more complicated Xpath expressions. For example, iterating through orders that get a 15% discount.
<loop expr="$orders/order[get-discount(.) =
'15%']">
</loop>
Or catching order events that get a 15% discount.
<var name="order">
<catch pattern="content/order[get-discount(.) =
'15%']"/>
</var>
Without the function, you would have to catch all order events in a loop until you found one that got the discount.
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]