Skip to content

How to link to another page using link to

macourtney edited this page Sep 13, 2010 · 3 revisions

Added for version: 0.4
Updated for version: 0.7

To link to page in your app use the link-to functions in your view. The link-to functions are part of conjure.core.view.base which is automatically used by all views created using the generate script.

Link-to

To link to the same page you’re on with link text “Click Here” use:

(link-to "Click Here")

To link to another action, list, in the same controller:

(link-to "Click Here" { :action :list })

Strings and keywords can be used interchangeably with the values of the params map. The above function call is equivalent to:

(link-to "Click Here" { :action "list" })

To link to another action, list, in a different controller, message, use:

(link-to "Click Here" { :controller "message", :action "list" })

To pass the parameter :id with value 1 to the action ‘show’ use:

(link-to "Click Here" { :action :show, :params { :id 1 } })

You can pass a function as the link text, and it will be evaluated with a map created by combining the request-map and params map passed to link-to. For example, the following link-to call:

(link-to #(str "Message " (:id (:params %))) { :action :show, :params { :id 1 } })

Will return a link with the text “Message 1”.

To set the attributes of the anchor link you can use the :html-options.

For example, to set the class of the anchor link returned by link-to, use something like:

(link-to "Link Class" { :html-options { :class "link-class" } })

You can use :html-options to override the url the link points to by adding the attribute :href.

For example, to point the link to “http://www.google.com” use:

(link-to "Google" { :html-options { :href "http://www.google.com" } })

Link-to-if and link-to-unless

If you want the link to be disabled under certain conditions you can use link-to-if.

To create a disabled link if ‘i’ is not equal to 2 use:

(link-to-if (= i 2) "Click Here")

All link-to parameters apply to link-to-if.

You can pass link-to-if a function which takes a map which is the combination of the request-map and parameters. If the function returns false, the link is disabled, otherwise it is enabled.

The following link:

(link-to-if #(= (:id (:params %)) 2) "Click Here" { :params { :id 2 } })

Is enabled only if the parameter :id is equal to 2 which it is.

Link-to-unless works just like link-to-if but on the inverse of the condition.

For example:

(link-to-unless (= i 2) "Click Here")

The above link-to-unless is enabled as long as i does not equal 2.

You can also pass a function to link-to-unless just like link-to-if.

Clone this wiki locally