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
.Helpers
17 using System
.Collections
;
18 using Castle
.MonoRail
.Framework
;
19 using Castle
.MonoRail
.Framework
.Services
;
22 /// Helper that allows the creation of urls using a dictionary.
25 /// For more information see <see cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
31 /// By default the urlhelper sets the encode to <c>true</c>, so the html generated is valid xhtml.
34 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
35 public class UrlHelper
: AbstractHelper
37 private IUrlBuilder urlBuilder
;
38 private UrlInfo currentUrl
;
42 /// Initializes a new instance of the <see cref="UrlHelper"/> class.
44 public UrlHelper() { }
46 /// Initializes a new instance of the <see cref="UrlHelper"/> class.
47 /// setting the Controller, Context and ControllerContext.
49 /// <param name="engineContext">The engine context.</param>
50 public UrlHelper(IEngineContext engineContext
) : base(engineContext
) { }
54 /// Gets or sets the URL builder.
56 /// <value>The URL builder.</value>
57 public IUrlBuilder UrlBuilder
59 get { return urlBuilder; }
60 set { urlBuilder = value; }
64 /// Gets or sets the current URL.
66 /// <value>The current URL.</value>
67 public UrlInfo CurrentUrl
69 get { return currentUrl; }
70 set { currentUrl = value; }
76 /// <param name="context">The context.</param>
77 public override void SetContext(IEngineContext context
)
79 base.SetContext(context
);
81 urlBuilder
= (IUrlBuilder
) context
.GetService(typeof(IUrlBuilder
));
82 currentUrl
= context
.UrlInfo
;
86 /// Outputs a path constructed using the specified parameters.
89 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
92 /// The following code uses nvelocity syntax:
95 /// $url.for("%{action='Save'}")
98 /// <para>outputs</para>
100 /// <code>/ControllerNameFromContext/Save.extension_configured</code>
103 /// $url.for("%{action='Edit',querystring='id=1'}")
106 /// <para>outputs</para>
108 /// <code>/ControllerNameFromContext/Edit.extension_configured?id=1</code>
111 /// <param name="parameters">The parameters.</param>
112 /// <returns></returns>
113 public string For(IDictionary parameters
)
115 SetEncodeDefault(parameters
);
117 UrlBuilderParameters urlParams
= UrlBuilderParameters
.From(parameters
).
118 SetRouteMatch(ControllerContext
.RouteMatch
);
120 return urlBuilder
.BuildUrl(currentUrl
, urlParams
);
124 /// Outputs an anchor element constructed using the specified parameters.
127 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
130 /// The following code uses nvelocity syntax:
133 /// $url.link('my link', "%{action='Save'}")
136 /// <para>outputs</para>
138 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured">my link</a> ]]> </code>
141 /// $url.link('my link', "%{action='Edit',querystring='id=1'}")
144 /// <para>outputs</para>
146 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Edit.extension_configured?id=1">my link</a> ]]> </code>
149 /// <param name="innerContent">The anchor text.</param>
150 /// <param name="parameters">The parameters.</param>
151 /// <returns></returns>
152 public string Link(string innerContent
, IDictionary parameters
)
154 return "<a href=\"" + For(parameters
) + "\">" + innerContent
+ "</a>";
158 /// Outputs an anchor element constructed using the specified parameters.
161 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
164 /// The following code uses nvelocity syntax:
167 /// $url.link('my link', "%{action='Save'}", "%{class='buttonlink'}")
170 /// <para>outputs</para>
172 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured" class="buttonlink">my link</a> ]]> </code>
176 /// <param name="innerContent">The anchor text.</param>
177 /// <param name="parameters">The parameters.</param>
178 /// <param name="anchorAttributes">The anchor element attributes.</param>
179 /// <returns></returns>
180 public string Link(string innerContent
, IDictionary parameters
, IDictionary anchorAttributes
)
182 return "<a " + GetAttributes(anchorAttributes
) + " href=\"" + For(parameters
) + "\">" + innerContent
+ "</a>";
186 /// Outputs a button element constructed using the specified parameters.
189 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
192 /// The following code uses nvelocity syntax:
195 /// $url.ButtonLink('my link', "%{action='Save'}")
198 /// <para>outputs</para>
200 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'">my link</a> ]]> </code>
204 /// <param name="innerContent">The button text.</param>
205 /// <param name="parameters">The parameters.</param>
206 /// <returns></returns>
207 public string ButtonLink(string innerContent
, IDictionary parameters
)
209 return "<button type=\"button\" onclick=\"javascript:window.location.href = '" + For(parameters
) + "'\">" + innerContent
+ "</button>";
213 /// Outputs a button element constructed using the specified parameters.
216 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
219 /// The following code uses nvelocity syntax:
222 /// $url.ButtonLink('my link', "%{action='Save'}", "%{class='buttonlink'}")
225 /// <para>outputs</para>
227 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'" class="buttonlink">my link</a> ]]> </code>
231 /// <param name="innerContent">The button text.</param>
232 /// <param name="parameters">The parameters.</param>
233 /// <param name="buttonAttributes">The button element attributes.</param>
234 /// <returns></returns>
235 public string ButtonLink(string innerContent
, IDictionary parameters
, IDictionary buttonAttributes
)
237 return "<button type=\"button\"" + GetAttributes(buttonAttributes
) + " onclick=\"javascript:window.location.href = '" + For(parameters
) + "'\">" + innerContent
+ "</button>";
240 private static void SetEncodeDefault(IDictionary parameters
)
242 if (!parameters
.Contains("encode"))
244 parameters
["encode"] = "true";