root / framework / trunk / extensions / lexicon / cf / case.cfm

Revision 122, 2.0 kB (checked in by scorfield, 3 years ago)

Addresses #19 by adding try/catch, switch/case/defaultcase and updating the skeleton code to use them.

Line 
1<cfscript>
2        // example custom verb that compiles to a <cfcase> tag
3        // usage:
4        // 1. add the lexicon declaration to your circuit file:
5        //    <circuit xmlns:cf="cf/">
6        // 2. use the verb in a fuseaction:
7        //    <cf:switch expression="#someExpr#">
8        //    <cf:case value="someValue">
9        //        ... some code ...
10        //    </cf:catch>
11        //    <cf:case value="anotherValue|andAnother" delimiters="|">
12        //        ... more code ...
13        //    </cf:case>
14        //    <cf:defaultcase>
15        //        ... default code ...
16        //    </cf:defaultcase>
17        //    </cf:switch>
18        //
19        // how this works:
20        // a. validate the attributes passed in (fb_.verbInfo.attributes)
21        // b. in start mode, generate the required CFML
22        // c. in end mode, generate the required CFML
23        //
24        if (fb_.verbInfo.executionMode is "start") {
25                //
26                // validate attributes:
27                // parent tag must be a switch verb in the same lexicon:
28                if (not structKeyExists(fb_.verbInfo,"parent") or
29                                fb_.verbInfo.parent.lexicon is not fb_.verbInfo.lexicon or
30                                fb_.verbInfo.parent.lexiconVerb is not "switch") {
31                        fb_throw("fusebox.badGrammar.invalidNesting",
32                                                "Verb is invalid in this context",
33                                                "The verb 'case' does not appear directly nested within a 'switch' verb in fuseaction #fb_.verbInfo.circuit#.#fb_.verbInfo.fuseaction#.");
34                }
35                //
36                // value is required:
37                if (not structKeyExists(fb_.verbInfo.attributes,"value")) {
38                        fb_throw("fusebox.badGrammar.requiredAttributeMissing",
39                                                "Required attribute is missing",
40                                                "The attribute 'value' is required, for a 'case' verb in fuseaction #fb_.verbInfo.circuit#.#fb_.verbInfo.fuseaction#.");
41                }
42                //
43                // delimiters is optional:
44                if (structKeyExists(fb_.verbInfo.attributes,"delimiters")) {
45                        fb_.delimiters = ' delimiters="#fb_.verbInfo.attributes.delimiters#"';
46                } else {
47                        fb_.delimiters = '';
48                }
49                //
50                // start mode:
51                fb_appendLine('<' & 'cfcase value="#fb_.verbInfo.attributes.value#"#fb_.delimiters#>');
52        } else {
53                //
54                // end mode:
55                fb_appendLine('<' & '/cfcase>');
56        }
57</cfscript>
Note: See TracBrowser for help on using the browser.