setting all protected methods on WizardStepPage as virtual
[castle.git] / MonoRail / Castle.MonoRail.Framework / RenderingSupport.cs
blobb5e03f8ae91dc32703600ddda403f2047b8fae05
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.Framework
17 using System;
18 using System.IO;
20 /// <summary>
21 /// Support operations for setting view and layout.
22 /// </summary>
23 /// <remarks>
24 /// <para>
25 /// This class holds the diverse RenderView, RenderSharedView and
26 /// RenderText family of methods originated from <see cref="Controller"/> along with CancelView and
27 /// CancelLayout. This has been done as <see cref="IDynamicAction"/>s do not have
28 /// access to the Controller itself, but only to <see cref="IController"/>,
29 /// <see cref="IEngineContext"/> and <see cref="IControllerContext"/>.
30 /// <see cref="RenderingSupport"/> allows dynamic actions to set the
31 /// relevant properties in a manner consistent to regular actions.
32 /// </para>
33 /// <example>
34 /// Code example:
35 /// <code>
36 /// public class ExampleDynAction : IDynamicAction
37 /// {
38 /// public object Execute(
39 /// IEngineContext engineContext,
40 /// IController controller,
41 /// IControllerContext controllerContext)
42 /// {
43 /// // other code
44 /// new RenderingSupport(controllerContext, engineContext)
45 /// .RenderView("myView");
46 /// return null;
47 /// }
48 /// }
49 /// </code>
50 /// </example>
51 /// <para>
52 /// Please note that this class does not implement the following rendering-relating
53 /// methods due to complex dependencies of Controller's instance data (mainly
54 /// <see cref="Controller.viewEngineManager"/>, which cannot be accessed by using the
55 /// information passed to DynamicActions):
56 /// <list type="bullet">
57 /// <item><description><see cref="Controller.DirectRender"/></description></item>
58 /// <item><description><see cref="Controller.InPlaceRenderView"/></description></item>
59 /// <item><description><see cref="Controller.InPlaceRenderSharedView"/></description></item>
60 /// <item><description><see cref="Controller.HasTemplate"/></description></item>
61 /// </list>
62 /// </para>
63 /// </remarks>
64 public class RenderingSupport
66 private readonly IControllerContext context;
67 private readonly IEngineContext engineContext;
69 /// <summary>
70 /// Instantiates RenderingSupport for the attached contexts.
71 /// </summary>
72 /// <param name="context">The controller context</param>
73 /// <param name="engineContext">The engine context</param>
74 public RenderingSupport(IControllerContext context, IEngineContext engineContext)
76 this.context = context;
77 this.engineContext = engineContext;
80 /// <summary>
81 /// Specifies the view to be processed after the action has finished its processing.
82 /// </summary>
83 /// <param name="name">view template name (the file extension is optional)</param>
84 public void RenderView(string name)
86 context.SelectedViewName = Path.Combine(context.ViewFolder, name);
89 /// <summary>
90 /// Specifies the view to be processed after the action has finished its processing.
91 /// </summary>
92 /// <param name="name">view template name (the file extension is optional)</param>
93 /// <param name="skipLayout">If set to <c>true</c>, no layout will be used when rendering the view</param>
94 public void RenderView(string name, bool skipLayout)
96 if (skipLayout) CancelLayout();
98 RenderView(name);
101 /// <summary>
102 /// Specifies the view to be processed after the action has finished its processing.
103 /// </summary>
104 /// <param name="name">view template name (the file extension is optional)</param>
105 /// <param name="skipLayout">If set to <c>true</c>, no layout will be used when rendering the view</param>
106 /// <param name="mimeType">The mime type to use on the reply</param>
107 public void RenderView(string name, bool skipLayout, string mimeType)
109 if (skipLayout) CancelLayout();
110 engineContext.Response.ContentType = mimeType;
112 RenderView(name);
115 /// <summary>
116 /// Specifies the view to be processed after the action has finished its processing.
117 /// </summary>
118 /// <param name="controller">Controller name get view from (if you intend to user another controller's view</param>
119 /// <param name="name">view template name (the file extension is optional)</param>
120 public void RenderView(string controller, string name)
122 context.SelectedViewName = Path.Combine(controller, name);
125 /// <summary>
126 /// Specifies the view to be processed after the action has finished its processing.
127 /// </summary>
128 /// <param name="controller">Controller name get view from (if you intend to user another controller's view</param>
129 /// <param name="name">view template name (the file extension is optional)</param>
130 /// <param name="skipLayout">If set to <c>true</c>, no layout will be used when rendering the view</param>
131 public void RenderView(string controller, string name, bool skipLayout)
133 if (skipLayout) CancelLayout();
135 RenderView(controller, name);
138 /// <summary>
139 /// Specifies the view to be processed after the action has finished its processing.
140 /// </summary>
141 /// <param name="controller">Controller name get view from (if you intend to user another controller's view</param>
142 /// <param name="name">view template name (the file extension is optional)</param>
143 /// <param name="skipLayout">If set to <c>true</c>, no layout will be used when rendering the view</param>
144 /// <param name="mimeType">The mime type to use on the reply</param>
145 public void RenderView(string controller, string name, bool skipLayout, string mimeType)
147 if (skipLayout) CancelLayout();
149 engineContext.Response.ContentType = mimeType;
150 RenderView(controller, name);
153 /// <summary>
154 /// Specifies the view to be processed after the action has finished its processing.
155 /// </summary>
156 /// <param name="controller">Controller name get view from (if you intend to user another controller's view</param>
157 /// <param name="name">view template name (the file extension is optional)</param>
158 /// <param name="mimeType">The mime type to use on the reply</param>
159 public void RenderView(string controller, string name, string mimeType)
161 engineContext.Response.ContentType = mimeType;
162 RenderView(controller, name);
165 /// <summary>
166 /// Specifies the shared view to be processed after the action has finished its
167 /// processing. (A partial view shared
168 /// by others views and usually in the root folder
169 /// of the view directory).
170 /// </summary>
171 public void RenderSharedView(string name)
173 context.SelectedViewName = name;
176 /// <summary>
177 /// Specifies the shared view to be processed after the action has finished its
178 /// processing. (A partial view shared
179 /// by others views and usually in the root folder
180 /// of the view directory).
181 /// </summary>
182 public void RenderSharedView(string name, bool skipLayout)
184 if (skipLayout) CancelLayout();
186 RenderSharedView(name);
189 /// <summary>
190 /// Cancels the view processing.
191 /// </summary>
192 public void CancelView()
194 context.SelectedViewName = null;
197 /// <summary>
198 /// Cancels the layout processing.
199 /// </summary>
200 public void CancelLayout()
202 context.LayoutNames = null;
205 /// <summary>
206 /// Cancels the view processing and writes
207 /// the specified contents to the browser
208 /// </summary>
209 public void RenderText(string contents)
211 CancelView();
213 engineContext.Response.Write(contents);
216 /// <summary>
217 /// Cancels the view processing and writes
218 /// the specified contents to the browser
219 /// </summary>
220 public void RenderText(string contents, params object[] args)
222 RenderText(String.Format(contents, args));
225 /// <summary>
226 /// Cancels the view processing and writes
227 /// the specified contents to the browser
228 /// </summary>
229 public void RenderText(IFormatProvider formatProvider, string contents, params object[] args)
231 RenderText(String.Format(formatProvider, contents, args));