- Implemented support for view component caching. Just use the attribute
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / CaptureFor.cs
blobd4ee004bef4353732f12257375217d91d55852b3
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);
53 String currentContent = Context.ContextVars[id] as string;
54 StringBuilder sb = buffer.GetStringBuilder();
55 String appendAtt = Context.ComponentParameters["append"] as string;
57 if (appendAtt != null)
59 if (appendAtt == "before")
61 sb.Append(currentContent);
63 else
65 sb.Insert(0, currentContent);
69 Context.ContextVars[id] = sb.ToString();