Posted in ColdFusion | Posted on 05-07-2008 | 5,256 views
Shimju asks:
In a model-glue app, suppose I have 3 templates files - templateA, templateB and templateC. All are set as private events in model-glue.xml. Now my requirement is I want to select a particular template dynamically based on Arguments.event.AddResult("templateA") which we set on contoller method for the event. Based on this, I want corresponding template should appear for that event. Can you please advice how we can accomplish this.
This is rather simple, and I think you already have most of the answer written out already. Typically folks do layouts in Model-Glue like so:
2 <broadcasts />
3 <results>
4 <result do="view.template" />
5 </results>
6 <views>
7 <include name="body" template="dspIndex.cfm" />
8 </views>
9</event-handler>
10
11<event-handler name="view.template">
12 <broadcasts />
13 <results />
14 <views>
15 <include name="template" template="dspTemplate.cfm" />
16 </views>
17</event-handler>
The event, page.index, has one result. Because it is unnamed, and the only result, it will always run. So all calls to page.index always run view.template. As you have already suggested, you can simply add other results. So let's say you want to have a default template, and two alternates, slim and print. Your controller could decide to add those results based on some logic, like the existence of a print attribute:
2 <cfset arguments.event.addResult("print")>
3</cfif>
The modified XML could then look like so:
2 <broadcasts />
3 <results>
4 <result name="print" do="view.printtemplate" />
5 <result do="view.template" />
6 </results>
7 <views>
8 <include name="body" template="dspIndex.cfm" />
9 </views>
10</event-handler>
11
12<event-handler name="view.template">
13 <broadcasts />
14 <results />
15 <views>
16 <include name="template" template="dspTemplate.cfm" />
17 </views>
18</event-handler>
19
20<event-handler name="view.printtemplate">
21 <broadcasts />
22 <results />
23 <views>
24 <include name="template" template="dspPrintTemplate.cfm" />
25 </views>
26</event-handler>
This simply reads as - if no result was fired, use the view.template template, but if the print result was used, fire off view.printtemplate.
Pretty simple, but I wonder if this could be done easier? (And yes, I'm being sly. Look later today for another example using Model-Glue 3, or as I call it, the Super-Fantastic Uber Framework.)


Later we may extend D2O to support some of those types of features... but that will be after 1.0 for sure. I think it is the way all of our IDE tools should move when able. We need some more advanced tools that go beyond text editing. Of course keep the text stuff friendly. This is a great post on that. My long term concern is always not what can be done friendly... but rather what won't be. (standard programmer paranoia.)
D20?
I find it makes things clearer in the xml as well if you omit the EMPTY broadcasts,views or results. I find that it's easier and quicker to read or skip thru
[Add Comment] [Subscribe to Comments]