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
.Framework
21 /// Support operations for setting view and layout.
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.
36 /// public class ExampleDynAction : IDynamicAction
38 /// public object Execute(
39 /// IEngineContext engineContext,
40 /// IController controller,
41 /// IControllerContext controllerContext)
44 /// new RenderingSupport(controllerContext, engineContext)
45 /// .RenderView("myView");
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>
64 public class RenderingSupport
66 private readonly IControllerContext context
;
67 private readonly IEngineContext engineContext
;
70 /// Instantiates RenderingSupport for the attached contexts.
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
;
81 /// Specifies the view to be processed after the action has finished its processing.
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
);
90 /// Specifies the view to be processed after the action has finished its processing.
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();
102 /// Specifies the view to be processed after the action has finished its processing.
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
;
116 /// Specifies the view to be processed after the action has finished its processing.
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
);
126 /// Specifies the view to be processed after the action has finished its processing.
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
);
139 /// Specifies the view to be processed after the action has finished its processing.
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
);
154 /// Specifies the view to be processed after the action has finished its processing.
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
);
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).
171 public void RenderSharedView(string name
)
173 context
.SelectedViewName
= name
;
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).
182 public void RenderSharedView(string name
, bool skipLayout
)
184 if (skipLayout
) CancelLayout();
186 RenderSharedView(name
);
190 /// Cancels the view processing.
192 public void CancelView()
194 context
.SelectedViewName
= null;
198 /// Cancels the layout processing.
200 public void CancelLayout()
202 context
.LayoutNames
= null;
206 /// Cancels the view processing and writes
207 /// the specified contents to the browser
209 public void RenderText(string contents
)
213 engineContext
.Response
.Write(contents
);
217 /// Cancels the view processing and writes
218 /// the specified contents to the browser
220 public void RenderText(string contents
, params object[] args
)
222 RenderText(String
.Format(contents
, args
));
226 /// Cancels the view processing and writes
227 /// the specified contents to the browser
229 public void RenderText(IFormatProvider formatProvider
, string contents
, params object[] args
)
231 RenderText(String
.Format(formatProvider
, contents
, args
));