1 // Copyright 2004-2007 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
;
41 /// Gets or sets the URL builder.
43 /// <value>The URL builder.</value>
44 public IUrlBuilder UrlBuilder
46 get { return urlBuilder; }
47 set { urlBuilder = value; }
51 /// Gets or sets the current URL.
53 /// <value>The current URL.</value>
54 public UrlInfo CurrentUrl
56 get { return currentUrl; }
57 set { currentUrl = value; }
61 /// Sets the controller.
63 /// <param name="controller">Current view's <see cref="AbstractHelper.Controller"/>.</param>
64 public override void SetController(Controller controller
)
66 base.SetController(controller
);
68 urlBuilder
= controller
.Context
.GetService
<IUrlBuilder
>();
69 currentUrl
= controller
.Context
.UrlInfo
;
73 /// Outputs a path constructed using the specified parameters.
76 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
79 /// The following code uses nvelocity syntax:
82 /// $url.for("%{action='Save'}")
85 /// <para>outputs</para>
87 /// <code>/ControllerNameFromContext/Save.extension_configured</code>
90 /// $url.for("%{action='Edit',querystring='id=1'}")
93 /// <para>outputs</para>
95 /// <code>/ControllerNameFromContext/Edit.extension_configured?id=1</code>
98 /// <param name="parameters">The parameters.</param>
99 /// <returns></returns>
100 public string For(IDictionary parameters
)
102 SetEncodeDefault(parameters
);
103 return urlBuilder
.BuildUrl(currentUrl
, parameters
);
107 /// Outputs an anchor element constructed using the specified parameters.
110 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
113 /// The following code uses nvelocity syntax:
116 /// $url.link('my link', "%{action='Save'}")
119 /// <para>outputs</para>
121 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured">my link</a> ]]> </code>
124 /// $url.link('my link', "%{action='Edit',querystring='id=1'}")
127 /// <para>outputs</para>
129 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Edit.extension_configured?id=1">my link</a> ]]> </code>
132 /// <param name="innerContent">The anchor text.</param>
133 /// <param name="parameters">The parameters.</param>
134 /// <returns></returns>
135 public string Link(string innerContent
, IDictionary parameters
)
137 SetEncodeDefault(parameters
);
138 return "<a href=\"" + For(parameters
) + "\">" + innerContent
+ "</a>";
142 /// Outputs an anchor element constructed using the specified parameters.
145 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
148 /// The following code uses nvelocity syntax:
151 /// $url.link('my link', "%{action='Save'}", "%{class='buttonlink'}")
154 /// <para>outputs</para>
156 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured" class="buttonlink">my link</a> ]]> </code>
160 /// <param name="innerContent">The anchor text.</param>
161 /// <param name="parameters">The parameters.</param>
162 /// <param name="anchorAttributes">The anchor element attributes.</param>
163 /// <returns></returns>
164 public string Link(string innerContent
, IDictionary parameters
, IDictionary anchorAttributes
)
166 SetEncodeDefault(parameters
);
167 return "<a " + GetAttributes(anchorAttributes
) + " href=\"" + For(parameters
) + "\">" + innerContent
+ "</a>";
171 /// Outputs a button element constructed using the specified parameters.
174 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
177 /// The following code uses nvelocity syntax:
180 /// $url.ButtonLink('my link', "%{action='Save'}")
183 /// <para>outputs</para>
185 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'">my link</a> ]]> </code>
189 /// <param name="innerContent">The button text.</param>
190 /// <param name="parameters">The parameters.</param>
191 /// <returns></returns>
192 public string ButtonLink(string innerContent
, IDictionary parameters
)
194 SetEncodeDefault(parameters
);
195 return "<button type=\"button\" onclick=\"javascript:window.location.href = '" + For(parameters
) + "'\">" + innerContent
+ "</button>";
199 /// Outputs a button element constructed using the specified parameters.
202 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
205 /// The following code uses nvelocity syntax:
208 /// $url.ButtonLink('my link', "%{action='Save'}", "%{class='buttonlink'}")
211 /// <para>outputs</para>
213 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'" class="buttonlink">my link</a> ]]> </code>
217 /// <param name="innerContent">The button text.</param>
218 /// <param name="parameters">The parameters.</param>
219 /// <param name="buttonAttributes">The button element attributes.</param>
220 /// <returns></returns>
221 public string ButtonLink(string innerContent
, IDictionary parameters
, IDictionary buttonAttributes
)
223 SetEncodeDefault(parameters
);
224 return "<button type=\"button\"" + GetAttributes(buttonAttributes
) + " onclick=\"javascript:window.location.href = '" + For(parameters
) + "'\">" + innerContent
+ "</button>";
227 private static void SetEncodeDefault(IDictionary parameters
)
229 if (!parameters
.Contains("encode"))
231 parameters
["encode"] = "true";