1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.ViewComponents
22 /// Renders the inner content and stores it in the IViewEngineContext
24 /// #blockcomponent(CaptureFor with "id=someId" ["append=before"])
25 /// content to be captured
30 /// id - the key to be used to retrieve the captured contents
31 /// append - when present will append component content into the current
32 /// content, if append = "before" will append before the current content
34 public class CaptureFor
: ViewComponent
37 /// Render component's content and stores it in the view engine ContextVars
38 /// so it can be reference and included in other places
40 public override void Render()
42 String id
= (String
)Context
.ComponentParameters
["id"];
44 if (id
== null || id
.Trim().Length
== 0)
46 throw new MonoRailException("CaptureFor requires an id attribute use #blockcomponent(CaptureFor with \"id=someid\")...#end");
49 StringWriter buffer
= new StringWriter();
51 Context
.RenderBody(buffer
);
54 String currentContent
= Context
.ContextVars
[id
] as string;
55 StringBuilder sb
= buffer
.GetStringBuilder();
56 String appendAtt
= Context
.ComponentParameters
["append"] as string;
58 if (appendAtt
!= null)
60 if (appendAtt
== "before")
62 sb
.Append(currentContent
);
66 sb
.Insert(0, currentContent
);
70 Context
.ContextVars
[id
] = sb
.ToString();
72 //This makes sure that Brail would bubble the content
73 //up to a parent view if called from a sub view.
74 Context
.ContextVars
[id
+ ".@bubbleUp"] = true;