开发者

What is ColdFusion Model Glue's equivalent to ASP.NET MVC 3's @section?

In ASP.NET MVC 3, you can have an @section within a view:

@section SideBar {
    <p>Some content</p>
    <p>Some more content</p>
}

<p>Body content</p>

Then in the master view, you would use this to render it:

<div id="sidebar">
    @RenderSection("SideBar", false)
</div>

@RenderBody()

What would be the ColdFusion equivalent of this in the Model Glue framework? I know I can set simple variables in the view:

<cfset event.setValue("section", "Tables")>

Then use them in the master template like so:

<cfif event.exists("section")><h3>#event.getValue("section")#</h3></cfif>

But this only works well for one-liners and simple strings. What I'd like to do is include an entire HTML block. What is the best way of accomplishing this? I think this would work in theory:

<cfsavecontent variable="sidebar">
    <p>Some content</p>
    <p>Some more content</p>
</cfsavecontent>

<cfset event.setValue("sidebar", sidebar)>

But I was wondering if there's a better way of doing it.

Edit:

In response to Adam Cameron's answer, Model Glue, from what I can tell, only supports the ability to combine separate files into one template:

SideBar.cfm:
<p>Some content</p>
<p>Some more content</p>

Page.cfm:
<p>Body content</p>

ModelGlue.xml:
<event-handler name="page.text">
    <views>
        <include name="sidebar" template="SideBar.cfm"/>
        <include name="body" template="Page.cfm"/>
        <include name="main" template="main.cfm"/>
    </views>
</event-handler>

main.cfm:
<cfoutput>#viewCollection.getView("sidebar")#</cfoutput>
<cfoutput>#viewCollection.getView("body")#</cfoutput>

I need to be able to declare the sidebar content within the page.cfm view. The thought here is that there will be a div somewhere in the main template that allows for a small HTML开发者_Python百科 snippet, say an image with a text description and a link, which any view can populate. It wouldn't make sense to have something like Page1.cfm and Page1SidebarContent.cfm, Page2.cfm and Page2SidebarContent.cfm, etc...


ModelGlue doesn't support what you want to do out of the box. However its easy enough to achieve using Peter's suggestion and ModelGlue helpers for encapsulation.

Create a new cfc, call it PageFragment.cfc and drop it in to your ModelGlue helpers directory.

// untested!
component name="PageFragment" {
 public boolean function exists(string name) {
  return structkeyexists(request.subcontent, arguments.name);
 }

 public string function get(string name) {
  if(exists(arguments.name)) return request.subcontent[arguments.name];
  return "";
 }

 public void function set(string name, string value) {
  request.subcontent[arguments.name] = arguments.value;
 }
}

Then in your views you can do

index.cfm

<cfset helpers.PageFragment.set("sidebar", "<p>My sidebar content</p>") />

main.cfm

<cfif helpers.PageFragment.exists("sidebar")>
  <div id="sidebar">#helpers.PageFragment.get("sidebar")#</div>
</cfif>

To avoid having to cfsavecontent all your fragments you could create a customtag that used thistag.generatedcontent and the caller scope to access your helpers.

By using helpers to encapsulate the functionality its really easy to reuse, or to change later without altering your views, for example you may want to add caching.


(Unfortunately) I haven't touched MG for ages, but I just googled the docs, as a reminder.

You need to read up on how views work, but this page of the docs summarises it succinctly:

http://docs.model-glue.com/wiki/ReferenceMaterials/ViewApi#ViewAPI

Specifically this code snippet:

<cfoutput>#viewcollection.getView("body")#</cfoutput> 

It's probably a case of reading through the docs a bit, and reminding yourself about how model glue's implementation of MVC (specifically the V part, in your case!) works.


I haven't used Model-Glue nor ASP.NET MVC, but it seems what you want can be achieved like this:

In page.cfm do:

<cfsavecontent variable="Request.SubContent['ThisPage'].Sidebar">
    <p>Some content</p>
    <p>Some more content</p>
</cfsavecontent>

<p>Body content</p>

Then in main.cfm use:

<div id="sidebar">
    <cfif StructKeyExists(Request.SubContent,PageName)
        AND StructKeyExists(Request.SubContent[PageName],'Sidebar')
        >
        #Request.SubContent[PageName].Sidebar#
    <cfelse>
        #viewCollection.getView("default_sidebar")#
    </cfif>
</div>

<cfoutput>#viewCollection.getView("body")#</cfoutput>

Depending on how things are structured, you might prefer to cache content in a persistent scope and/or hide it behind a couple of methods (possibly even extending Model-Glue to allow this natively; it is Open Source after all), but hopefully this gives a general idea?


I see what you mean now. I'm not sure MG will have that sort of functionality built-in because in an MVC environment, it's not really up to a view to be doing this sort of thing: you're kinda coupling controller & model stuff together in a view file. There might be a good reason for you doing this, but I wonder if it's your approach that might be your undoing here? Can you not put the "get the sidebar" stuff into the controller & have that call a model, and add in a sidebar view if needed? That'd be how I'd approach this.

That said, I know it's unhelpful having people say "I'm not going to answer your question, I'm just going to complain about it", so I'll have a look-see around and see if I can come up with something.

However given you're wanting to break-out of a MVC approach here, perhaps don't try to get MG to do this for you, just do what Peter suggests and capture a variable in the page.cfm view, stick it in a sensibly-structured struct (nice tautology, Cameron), and then look for it in the view you want to render it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜