Frank asks:
Hey Ray,
I've been using a variant of Fusebox for most of my career, and have decided to pick up Model-Glue. A quick question: using MG, can one build all of the content as variables and then "insert" them into templates, and filter the template?
Asked otherwise: Fusebox has allowed me to build the pages in chunks, using cfsavecontent and simply replace [[some_chunk_of_text]] in a template with the contents of that chunk of text, then finally run filters on the whole shebang such as filtering out white space between tags.
Is something like this reasonably easy to set up using MG?
So this is two questions in one really. First, can we combine multiple views into one, and secondly, can we do operations on those views. The first one is simple. Model-Glue has always supported the ability to combine multiple views into one result sent back to the browser. Within any Model-Glue request, you can have multiple view statements defined in your event:
<views>
<include name="body" template="pages/index.cfm" />
<include name="main" template="templates/main.cfm" />
</views>
Model-Glue will run both views, but only the result of the last one is sent to the screen. In order to create a combination you simply use the viewCollection API. So consider main.cfm, our template. It has access to the earlier views by using this:
viewCollection.getView("body")
So a typical template might then look like this:
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css"></link>
</head> <body>
<div id="banner">Dynamic View</div> <!--- Display the view named "body" --->
<cfoutput>#viewCollection.getView("body")#</cfoutput> </body> </html>
<html>
You can have more than two views of course. Your final view would simply add more calls to the collection to display them.
So again - thats pretty simple. Frank's second question is pretty interesting. He wants to know if we can operations on the views. It never really occurred to me to ever do that, but it definitely is possible. Here is a very simple example. Imagine we have a view which returns this:
<p>
The time is {currenttime}.
</p>
<p>
Model-Glue 3 seems to be up and running. Have fun!
</p>
Note the use of {currenttime} in the output. Now let's look at our template.
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css"></link>
</head> <body>
<div id="banner">Dynamic View</div> <!--- Display the view named "body" --->
<cfoutput>#body#</cfoutput> </body> </html>
<cfset body = viewCollection.getView("body")>
<cfset body = replaceNoCase(body, "{currenttime}", dateFormat(now(),"short") & " " & timeFormat(now(), "short") , "all")>
<html>
I've changed the code to not just output the earlier view, but to get it and perform a simple replace on it. Now obviously this is kind of a dumb example. But it does demonstrate that you can take earlier views and change them. As I said, I've never done that before in a production application, but maybe some of my readers have?