weavejester / hiccup

Fast library for rendering HTML in Clojure
http://weavejester.github.io/hiccup
Eclipse Public License 1.0
2.69k stars 177 forks source link

url, link-to, and with-base-url are incompatible #30

Closed brokensandals closed 12 years ago

brokensandals commented 13 years ago

Because url and link-to both call resolve-uri, code such as (link-to (url "/foo" {...})...) will result in the base URL being prepended twice.

Maybe I'm using the methods in a way they aren't intended? url is very convenient - helpful for generating links as well as redirect locations - and in my (very limited) time using Hiccup, almost every occasion I'm using link-to for a relative path, I also want to use url.

weavejester commented 13 years ago

You have a point. Perhaps link-to should not call resolve-uri, but leave that up to url. It's perhaps better to be explicit in this case.

brokensandals commented 13 years ago

That would be my vote. (I ran into the same issue with form-to.)

weavejester commented 12 years ago

I've fixed this in a way that both link-to and url can be used together with base URLs:

(with-base-url "/foo" (html (link-to "/bar" "Bar")))
=> "<a href=\"/foo/bar\">Bar</a>"

(with-base-url "/foo" (html (link-to (url "/bar") "Bar")))
=> "<a href=\"/foo/bar\">Bar</a>"

The one caveat is that with-base-url needs to be outside the html form, however, this is often the case.