Wiki source code of WOOgnl Framework

Version 21.1 by Pascal Robert on 2012/08/27 13:18

Show last authors
1 == Overview ==
2
3 OGNL stands for "Object Graph Navigation Language", and it defines an entire family of key-value coding-like abilities. As Jonathan Rentzsch put it in his CAWUG presentation on Project WOnder, "Think: Key-Value Coding on Steroids". You can get more information on the specifics on OGNL at [[the official OGNL website>>url:http://commons.apache.org/ognl/||shape="rect"]].
4
5 WOOgnl provides a framework that integrates the OGNL syntax into WO's standard binding resolution. By simply including the WOOgnl framework on your build path and preceding your binding value with a "~~", it will be interpreted by WOOgnl.
6
7 Here are some examples that demonstrate just a tiny bit of the really cool things you can do:
8
9 * value="~~\"Hello Mr.\" + session.user.firstName";
10 * value="~~name.length().(#this>100?2*#this:20+#this)";
11 * value="~~#A=new NSMutableArray(),#A.addObject(name),#A";
12
13 Here are some examples provided by Max Muller, WOOgnl's original author:
14
15 {{code}}
16 // Calling methods with arguments
17 Repetition1: WORepetition {
18 item = arrayItem;
19 list = "~sort(anArray, \"name\")";
20
21 {{/code}}
22
23 {{code}}
24 // Calling static methods
25 Repetition2: WORepetition {
26 item = arrayItem;
27 list = "~@er.extensions.ERXArrayUtilities@sortedArraySortedWithKey(anArray, \"name\")";
28 }
29 {{/code}}
30
31 {{code}}
32 // Accessing static ivars
33 String1: WOString {
34 value = "~@ognl.webobjects.WOOgnl@OgnlSpecialCharacters";
35 }
36 {{/code}}
37
38 {{code}}
39 // Accessing static ivars within inner class
40 String1: WOString {
41 value = "~@ognl.webobjects.WOOgnl$MyInnerClass@OgnlSpecialCharacters";
42 }
43 {{/code}}
44
45 {{code}}
46 // Use of conditionals, note that every previous value of the . is
47 // pushed into the ivar #this
48 String2: WOString {
49 value = "~name.length().(#this > 100? 2*#this : 20+#this)";
50 }
51 {{/code}}
52
53 {{code}}
54 // String concat
55 String3: WOString {
56 value = "~\"Hello Max \" + name";
57
58 {{/code}}
59
60 {{code}}
61 // Use of set operator in. can also use in against NSArray and
62 NSSet objects
63 String4: WOString {
64 value = "~name in {\"Main\", \"Something\"} ? \"Yes\" : \"No\"";
65 }
66 {{/code}}
67
68 {{code}}
69
70 // Variable declaration. Note that commas allow multiple actions
71 // per expression.
72 String5: WOString {
73 value = "~#A=new com.webobjects.foundation.NSMutableArray(),#A.addObject(name), #A.addObjectsFromArray(session.languages), #A";
74 }
75
76 {{/code}}
77
78 === Helper system ===
79
80 For more detail, check [[doc:WOOGNL Helper Functions]]. With these two lines in your properties file and Project WONDER,
81
82 {{code}}
83
84 ognl.helperFunctions=true
85 ognl.inlineBindings=true
86
87 {{/code}}
88
89 You can declare your own helper class like this:
90
91 {{code}}
92
93 public class StringHelper {
94 public String capitalize(String str) {
95 // ...
96 }
97 }
98
99 {{/code}}
100
101 And use that in your binding by "piping your data"
102
103 {{code}}
104
105 <wo:str value="$person.name|capitalize" />
106
107 {{/code}}