NO MORE CAREER
POLITICIANS!
Get Out Of Our House: Replacing congress with TRUE citizens!
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Web Hosting

best web hosting - top web hosting sites, thetop10bestwebhosting.com

Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< September, 2010 >>
SMTWTFS
1234
567891011
12131415161718
19202122232425
2627282930
Search Blog

Recent Comments
Re: Railo 3.1 on Windows Server 2008 and IIS7 - Part 3 of 3 (by Jon at 8/27 2:04 PM)
Re: Hosts File Changes Not Acknowledged on Vista 64 (by Spacy at 8/24 3:46 PM)
Re: THE DAY CFUNITED DIED (by ComboFusion at 8/23 10:50 AM)
Re: My Grandpa (by Tasha at 8/10 4:29 PM)
Re: Just What IS a 'Service Layer', Anyway? (by dougboude at 8/02 10:10 AM)
Re: Just What IS a 'Service Layer', Anyway? (by Isaac at 8/02 2:25 AM)
Re: PayPal IPN Coldfusion CFC (by Soyestudiambre at 7/25 6:12 PM)
Re: PHP vs COLDFUSION (by Tony Garcia at 7/17 11:24 AM)
Re: PHP vs COLDFUSION (by dougboude at 7/14 8:45 AM)
Re: PHP vs COLDFUSION (by Lola LB at 7/14 5:51 AM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

27 July 2007
Model Glue Views Demystified
At first, getting a grasp on how to "think" about rendered views in Model Glue may appear to be challenging; but I tell you, that you already know exactly how to think about them. If you have EVER included a template using <CFINCLUDE>, then you have a two minute learning curve to working with views in Model Glue.

Breaking your page up into individual pieces, and then including those pieces later on as needed is not a new thing for most of us. That is precisely what Model Glue allows us to do as well. Is it ever required that we divide our code into individual templates? Nay, and neither does Model Glue require it. It is, however, a very good idea in most instances, and MG gives us a very easy way to do this.

Picture if you will, a template.

We have dsp_main.cfm that will act as our layout, containing only DIV tags with appropriate IDs. Dsp_main.cfm also includes our external style sheet to provide appropriate positioning and look and feel to whatever content we put into those DIVs. Before Model Glue, we'd do it like this:

<html>
<head>
    <title>My Composite Template</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css" media="screen" />
</head>

<body>

<div id="header"><cfinclude template="dsp_header.cfm"></div>
<div id="nav"><cfinclude template="dsp_nav.cfm"></div>
<div id="body"><cfinclude template="dsp_body.cfm"></div>
<div id="footer"><cfinclude template="dsp_footer.cfm"></div>

</body>
</html>


Nothing new there, right? With Model Glue, the same dsp_main.cfm looks like this:

<!--- grab my rendered views out of the viewcollection... --->
<cfset header = viewCollection.getView("header") />
<cfset footer = viewCollection.getView("footer") />
<cfset body = viewCollection.getView("body") />
<cfset nav = viewCollection.getView("nav") />

<html>
<head>
    <title>MG View Demo</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css" media="screen" />
</head>

<body>

<div id="header"><cfoutput>#header#</cfoutput></div>
<div id="nav"><cfoutput>#nav#</cfoutput></div>
<div id="body"><cfoutput>#body#</cfoutput></div>
<div id="footer"><cfoutput>#footer#</cfoutput></div>

</body>
</html>


With Model Glue, rather than include those templates within the page itself, the templates were pre-rendered (almost exactly like using a CFSAVECONTENT), the rendered html stuffed into what's called the ViewCollection, and then we simply grab that rendered HTML out of the ViewCollection and output it in a very familiar fashion.

Now, how were those individual templates included? We told Model Glue to do it within the Modelglue.XML file that defines our individual events. For instance, let's say the event we called was 'main.landing' (http://www.mysite.com/index.cfm?event=main.landing). The relevant XML looks like this:

<event-handler name="main.landing">
    <broadcasts />
    <results />
    <views>
        <include name="body" template="dsp_content.cfm" /><!-- available in the viewcollection as 'body' -->
        <include name="nav" template="dsp_nav.cfm" /><!-- available in the viewcollection as 'nav' -->
        <include name="footer" template="dsp_footer.cfm" /><!-- available in the viewcollection as 'footer' -->
        <include name="header" template="dsp_header.cfm" /><!-- available in the viewcollection as 'header' -->
        <include name="main" template="dsp_main.cfm" /><!-- container template to layout individual parts -->
    </views>
</event-handler>


Using the 'include' tag within the 'views' tag, we told Modelglue which templates to render. Of note is the fact that the order in which they are rendered is completely irrelevant, WITH ONE EXCEPTION: the template that acts as the layout container MUST BE RENDERED LAST.

Also of note is the fact that each of your view templates should act independently of one another. For example, let's say that a Model Glue variable is needed in the dsp_nav.cfm and that same variable is also used somewhere in the dsp_content.cfm template. It would be bad practice for you to retrieve that variable within dsp_nav.cfm and then attempt to access the retrieved instance from dsp_content.cfm; each template should be retrieving it for themselves.

That's it, boys and girls. Nothing to it, and very little difference from the way you've been using <CFINCLUDE> all along!



Posted by dougboude at 12:01 PM | PRINT THIS POST! |Link | 4 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Model Glue Views Demystified
Doug, I'm only starting to get into MG but this is the kind of explanation that I couldn't seem to find anywhere else. Simplified and concise... I get it now.

Thanks,
Max
Posted by Max on July 29, 2007 at 9:32 AM

Re: Model Glue Views Demystified
Hi Max. I'm very glad you found the explanation useful! I've had to learn a lot of this stuff the hard way (banging my head against it, reading every tidbit I could find, then assimilating it all), so it is my personal policy to share what I know. As I become aware of things I've learned that I feel could help somebody else I'll be sure and post them!

Doug :0)
Posted by dougboude on July 29, 2007 at 10:25 PM

Re: Model Glue Views Demystified
Could not have explained it better if I tried. Your MG:Blog is fast becoming a prerequisite reading list for my team. Cheers
Posted by J Nanda on August 4, 2007 at 4:32 AM

Re: Model Glue Views Demystified
Could not have explained it better if I tried. Your MG:Blog is fast becoming a prerequisite reading list for my team. Cheers
Posted by J Nanda on August 4, 2007 at 4:33 AM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

20 plus 5 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!