Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / CaptureFor.cs
blob5b177e051b4a4fb5dd81239aa905c04c2afef28d
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.IO;
19 using System.Text;
21 /// <summary>
22 /// Renders the inner content and stores it in the IViewEngineContext
23 /// <code>
24 /// #blockcomponent(CaptureFor with "id=someId" ["append=before"])
25 /// content to be captured
26 /// #end
27 ///
28 /// ${someId}
29 /// </code>
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
33 /// </summary>
34 public class CaptureFor : ViewComponent
36 /// <summary>
37 /// Render component's content and stores it in the view engine ContextVars
38 /// so it can be reference and included in other places
39 /// </summary>
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);
64 else
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;