Wiki source code of WOOgnl Framework
Last modified by chuckhill on 2012/08/27 13:18
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
10.1 | 1 | == Overview == |
| |
6.1 | 2 | |
| |
21.1 | 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"]]. |
| |
6.1 | 4 | |
| |
21.1 | 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.1 | 6 | |
| 7 | Here are some examples that demonstrate just a tiny bit of the really cool things you can do: | ||
| 8 | |||
| |
21.1 | 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"; | ||
| |
6.1 | 12 | |
| 13 | Here are some examples provided by Max Muller, WOOgnl's original author: | ||
| 14 | |||
| |
22.2 | 15 | {{code}}// Calling methods with arguments |
| |
10.1 | 16 | Repetition1: WORepetition { |
| 17 | item = arrayItem; | ||
| 18 | list = "~sort(anArray, \"name\")"; | ||
| |
22.2 | 19 | } {{/code}} |
| |
6.1 | 20 | |
| |
22.2 | 21 | {{code}}// Calling static methods |
| |
10.1 | 22 | Repetition2: WORepetition { |
| 23 | item = arrayItem; | ||
| 24 | list = "~@er.extensions.ERXArrayUtilities@sortedArraySortedWithKey(anArray, \"name\")"; | ||
| |
22.2 | 25 | }{{/code}} |
| |
6.1 | 26 | |
| |
22.2 | 27 | {{code}}// Accessing static ivars |
| |
10.1 | 28 | String1: WOString { |
| 29 | value = "~@ognl.webobjects.WOOgnl@OgnlSpecialCharacters"; | ||
| |
22.2 | 30 | }{{/code}} |
| |
6.1 | 31 | |
| |
22.2 | 32 | {{code}}// Accessing static ivars within inner class |
| |
18.1 | 33 | String1: WOString { |
| 34 | value = "~@ognl.webobjects.WOOgnl$MyInnerClass@OgnlSpecialCharacters"; | ||
| |
22.2 | 35 | }{{/code}} |
| |
18.1 | 36 | |
| |
22.2 | 37 | {{code}}// Use of conditionals, note that every previous value of the . is |
| |
10.1 | 38 | // pushed into the ivar #this |
| 39 | String2: WOString { | ||
| 40 | value = "~name.length().(#this > 100? 2*#this : 20+#this)"; | ||
| |
22.2 | 41 | }{{/code}} |
| |
6.1 | 42 | |
| |
22.2 | 43 | {{code}}// String concat |
| |
10.1 | 44 | String3: WOString { |
| 45 | value = "~\"Hello Max \" + name"; | ||
| |
22.2 | 46 | } {{/code}} |
| |
6.1 | 47 | |
| |
22.2 | 48 | {{code}}// Use of set operator in. can also use in against NSArray and |
| |
10.1 | 49 | NSSet objects |
| 50 | String4: WOString { | ||
| 51 | value = "~name in {\"Main\", \"Something\"} ? \"Yes\" : \"No\""; | ||
| |
22.2 | 52 | }{{/code}} |
| |
6.1 | 53 | |
| |
10.1 | 54 | {{code}} |
| |
6.1 | 55 | |
| |
16.1 | 56 | // Variable declaration. Note that commas allow multiple actions |
| |
10.1 | 57 | // per expression. |
| 58 | String5: WOString { | ||
| 59 | value = "~#A=new com.webobjects.foundation.NSMutableArray(),#A.addObject(name), #A.addObjectsFromArray(session.languages), #A"; | ||
| 60 | } | ||
| |
6.1 | 61 | |
| |
16.1 | 62 | {{/code}} |
| |
12.1 | 63 | |
| |
16.1 | 64 | === Helper system === |
| 65 | |||
| |
23.1 | 66 | For more detail, check [[doc:documentation.Home.WOLips Tutorials.WOOGNL Helper Functions.WebHome]]. With these two lines in your properties file and Project WONDER, |
| |
16.1 | 67 | |
| 68 | {{code}} | ||
| 69 | |||
| |
18.1 | 70 | ognl.helperFunctions=true |
| |
16.1 | 71 | ognl.inlineBindings=true |
| 72 | |||
| |
12.1 | 73 | {{/code}} |
| |
16.1 | 74 | |
| 75 | You can declare your own helper class like this: | ||
| 76 | |||
| 77 | {{code}} | ||
| 78 | |||
| 79 | public class StringHelper { | ||
| 80 | public String capitalize(String str) { | ||
| |
18.1 | 81 | // ... |
| |
16.1 | 82 | } |
| 83 | } | ||
| 84 | |||
| 85 | {{/code}} | ||
| 86 | |||
| 87 | And use that in your binding by "piping your data" | ||
| 88 | |||
| 89 | {{code}} | ||
| 90 | |||
| 91 | <wo:str value="$person.name|capitalize" /> | ||
| 92 | |||
| 93 | {{/code}} |