More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / BrailViewComponentContext.cs
blob22d18a6d2db991fc014b04286d7d146a7ac9d286
1 // Copyright 2004-2008 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;
18 using System.Collections;
19 using System.IO;
20 using Boo.Lang;
21 using Framework;
23 public class BrailViewComponentContext : IViewComponentContext
25 private readonly TextWriter default_writer;
26 private ICallable body;
27 private string componentName;
29 private IDictionary componentParameters;
30 private BrailBase parent;
31 private IDictionary sections;
32 private string viewToRender;
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 = IgnoreNull.ReplaceIgnoreNullsWithTargets(parameters);
52 public ICallable Body
54 get { return body; }
55 set { body = value; }
58 #region IViewComponentContext Members
60 public string ComponentName
62 get { return componentName; }
65 public IDictionary ContextVars
67 get { return parent.Properties; }
70 public IDictionary ComponentParameters
72 get { return componentParameters; }
75 public string ViewToRender
77 get { return viewToRender; }
78 set { viewToRender = value; }
81 public TextWriter Writer
83 get { return default_writer; }
86 public void RenderBody()
88 RenderBody(default_writer);
91 public void RenderBody(TextWriter writer)
93 if (body == null)
95 throw new MonoRailException("This component does not have a body content to be rendered");
97 using(parent.SetOutputStream(writer))
99 body.Call(new object[] {writer});
103 /// <summary>
104 /// Pendent
105 /// </summary>
106 /// <param name="name"></param>
107 /// <param name="writer"></param>
108 public void RenderView(string name, TextWriter writer)
110 parent.OutputSubView(name, writer, new Hashtable());
113 public bool HasSection(string sectionName)
115 return sections != null && sections.Contains(sectionName);
118 public void RenderSection(string sectionName)
120 RenderSection(sectionName, default_writer);
123 /// <summary>
124 /// Renders the the specified section
125 /// </summary>
126 /// <param name="sectionName">Name of the section.</param>
127 /// <param name="writer">The writer.</param>
128 public void RenderSection(string sectionName, TextWriter writer)
130 if (HasSection(sectionName) == false)
131 return; //matching the NVelocity behavior, but maybe should throw?
132 ICallable callable = (ICallable) sections[sectionName];
133 callable.Call(new object[] {writer});
136 public IViewEngine ViewEngine
138 get { return parent.ViewEngine; }
141 #endregion
143 public void RegisterSection(string name, ICallable section)
145 if (sections == null)
147 sections = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
149 sections[name] = section;