Ask a Jedi: Multiple templates and Model-Glue

This post is more than 2 years old.

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: <event-handler name="page.index"> <broadcasts /> <results> <result do="view.template" /> </results> <views> <include name="body" template="dspIndex.cfm" /> </views> </event-handler>

<event-handler name="view.template"> <broadcasts /> <results /> <views> <include name="template" template="dspTemplate.cfm" /> </views> </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:

<cfif arguments.event.valueExists("print")> <cfset arguments.event.addResult("print")> </cfif>

The modified XML could then look like so:

<event-handler name="page.index"> <broadcasts /> <results> <result name="print" do="view.printtemplate" /> <result do="view.template" /> </results> <views> <include name="body" template="dspIndex.cfm" /> </views> </event-handler>

<event-handler name="view.template"> <broadcasts /> <results /> <views> <include name="template" template="dspTemplate.cfm" /> </views> </event-handler>

<event-handler name="view.printtemplate"> <broadcasts /> <results /> <views> <include name="template" template="dspPrintTemplate.cfm" /> </views> </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.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Shimju David posted on 5/7/2008 at 6:23 PM

Hey Rey, this is such an easy stuff and Iam surprised it didnt went over my head:)

Comment 2 by John Farrar posted on 5/7/2008 at 7:16 PM

It could most definitely be done easier... where is the graphical IDE for this stuff? Compare editing databases with graphical UI to doing everything in code. (Especially relationships.) I will enjoy seeing what Joe or someone can do with AIR or another tool to come up to speed on this way of making it easier.

Comment 3 by Raymond Camden posted on 5/7/2008 at 7:45 PM

John, have you looked at the Frameworks Explorer that ships w/ CFEclipse?

Comment 4 by John Farrar posted on 5/7/2008 at 8:13 PM

Not with Model Glue. Someone should do a YouTube or hook up with Charlie on that if it can do the job. It would be a big boost for the MG community. :)

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.)

Comment 5 by Raymond Camden posted on 5/7/2008 at 8:21 PM

Sorry John - are you asking for an IDE to help work with frameworks, or videos to help you learn how to use it? Seems like two different requests.

D20?

Comment 6 by zac spitzer posted on 5/8/2008 at 11:46 AM

I bet it's something about the format attribute in MG 3 :)

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

Comment 7 by Raymond Camden posted on 5/8/2008 at 3:03 PM

Actually I was going to demo eventtypes, but I ran into a few bugs. One is already fixed, one I'm waiting to hear back from Joe. The fun of working with Alpha software. ;)