Ask a Jedi: Multiple templates and Model-Glue

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> </code>

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> </code>

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 developer advocate. He focuses on JavaScript, serverless 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

Comments