1 // Copyright 2004-2008 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
.Views
.Brail
18 using System
.Collections
;
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
;
35 /// Initializes a new instance of the <see cref="BrailViewComponentContext"/> class.
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
)
48 default_writer
= text
;
49 componentParameters
= IgnoreNull
.ReplaceIgnoreNullsWithTargets(parameters
);
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
)
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}
);
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
);
124 /// Renders the the specified section
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; }
143 public void RegisterSection(string name
, ICallable section
)
145 if (sections
== null)
147 sections
= new Hashtable(StringComparer
.InvariantCultureIgnoreCase
);
149 sections
[name
] = section
;