- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / BrailViewComponentContext.cs
blob430f1b1b9704518d5dc46360790d4007a16a242c
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.Views.Brail
17 using System.Collections;
18 using System.IO;
19 using Boo.Lang;
20 using Castle.MonoRail.Framework;
22 public class BrailViewComponentContext : IViewComponentContext
24 string componentName;
26 IDictionary componentParameters;
27 IDictionary sections;
28 string viewToRender;
30 ICallable body;
31 private readonly TextWriter default_writer;
32 private BrailBase parent;
34 /// <summary>
35 /// Initializes a new instance of the <see cref="BrailViewComponentContext"/> class.
36 /// </summary>
37 /// <param name="parent">The parent.</param>
38 /// <param name="body">The body.</param>
39 /// <param name="name">The name.</param>
40 /// <param name="text">The text.</param>
41 /// <param name="parameters">The parameters.</param>
42 public BrailViewComponentContext(BrailBase parent, ICallable body,
43 string name, TextWriter text, IDictionary parameters)
45 this.parent = parent;
46 this.body = body;
47 componentName = name;
48 default_writer = text;
49 componentParameters = parameters;
52 public string ComponentName
54 get { return componentName; }
57 public IDictionary ContextVars
59 get { return parent.Properties; }
62 public IDictionary ComponentParameters
64 get { return componentParameters; }
67 public string ViewToRender
69 get { return viewToRender; }
70 set { viewToRender = value; }
73 public ICallable Body
75 get { return body; }
76 set { body = value; }
79 public TextWriter Writer
81 get { return default_writer; }
84 public void RenderBody()
86 RenderBody(default_writer);
89 public void RenderBody(TextWriter writer)
91 if (body == null)
93 throw new MonoRailException("This component does not have a body content to be rendered");
95 using (parent.SetOutputStream(writer))
97 body.Call(new object[] { writer });
101 /// <summary>
102 /// Pendent
103 /// </summary>
104 /// <param name="name"></param>
105 /// <param name="writer"></param>
106 public void RenderView(string name, TextWriter writer)
108 parent.OutputSubView(name, writer, new Hashtable());
111 public bool HasSection(string sectionName)
113 return sections != null && sections.Contains(sectionName);
116 public void RenderSection(string sectionName)
118 RenderSection(sectionName, default_writer);
121 /// <summary>
122 /// Renders the the specified section
123 /// </summary>
124 /// <param name="sectionName">Name of the section.</param>
125 /// <param name="writer">The writer.</param>
126 public void RenderSection(string sectionName, TextWriter writer)
128 if (HasSection(sectionName) == false)
129 return;//matching the NVelocity behavior, but maybe should throw?
130 ICallable callable = (ICallable)sections[sectionName];
131 callable.Call(new object[] { writer });
134 public IViewEngine ViewEngine
136 get { return parent.ViewEngine; }
139 public void RegisterSection(string name, ICallable section)
141 if (sections == null)
143 sections = new Hashtable(System.StringComparer.InvariantCultureIgnoreCase);
145 sections[name] = section;