Packages
phoenix_html
1.0.0
4.3.0
4.2.1
4.2.0
4.1.1
4.1.0
4.0.0
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.0
3.1.0
3.0.4
3.0.3
retired
3.0.2
retired
3.0.1
retired
3.0.0
retired
2.14.3
2.14.2
2.14.1
2.14.0
2.13.4
2.13.3
2.13.2
2.13.1
2.13.0
2.12.0
2.11.2
2.11.1
2.11.0
2.10.5
2.10.4
2.10.3
2.10.2
2.10.1
2.10.0
2.9.3
2.9.2
2.9.1
2.9.0
2.8.0
2.7.0
2.7.0-dev
2.6.2
2.6.1
2.6.0
2.5.1
2.5.0
2.4.0
2.4.0-dev
2.3.1
2.3.0
2.2.0
2.1.2
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-dev
1.4.0
1.3.0
1.2.1
1.2.0
1.1.0
1.0.1
1.0.0
Phoenix view functions for working with HTML templates
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/phoenix_html/link.ex
defmodule Phoenix.HTML.Link do
@moduledoc """
Conveniences for working with links and URLs in HTML.
"""
import Phoenix.HTML.Tag
@doc """
Generates a link to the given URL.
## Examples
link("hello", to: "/world")
#=> <a href="/world">hello</a>
link("<hello>", to: "/world")
#=> <a href="/world"><hello></a>
link("<hello>", to: "/world", class: "btn")
#=> <a class="btn" href="/world"><hello></a>
## Options
* `:to` - the page to link to. This option is required
* `:method` - the method to use with the link. In case the
method is not `:get`, the link is generated inside the form
which sets the proper information. In order to submit the
form, JavaScript must be enabled
* `:form` - customize the underlying form when the method
is not `:get`
All other options are forwarded to the underlying `<a>` tag.
"""
def link(text, opts) do
{to, opts} = Keyword.pop(opts, :to)
{method, opts} = Keyword.pop(opts, :method, :get)
unless to do
raise ArgumentError, "option :to is required in link/2"
end
if method == :get do
content_tag(:a, text, [href: to] ++ opts)
else
{form, opts} = form_options(opts, method, "link")
form_tag(to, form) do
content_tag(:a, text, [href: "#", onclick: "this.parentNode.submit(); return false;"] ++ opts)
end
end
end
@doc """
Generates a button that uses a regular HTML form to submit to the given URL.
Useful to ensure that links that change data are not triggered by
search engines and other spidering software.
## Examples
button("hello", to: "/world")
#=> <form action="/world" class="button" method="post">
<input name="_csrf_token" value=""><input type="submit" value="hello">
</form>
button("hello", to: "/world", method: "get", class: "btn")
#=> <form action="/world" class="btn" method="post">
<input type="submit" value="hello">
</form>
## Options
* `:to` - the page to link to. This option is required
* `:method` - the method to use with the button. Defaults to :post.
* `:form` - the options for the form. Defaults to
`[class: "button", enforce_utf8: false]`
All other options are forwarded to the underlying button input.
"""
def button(text, opts) do
{to, opts} = Keyword.pop(opts, :to)
{method, opts} = Keyword.pop(opts, :method, :post)
{form, opts} = form_options(opts, method, "button")
opts =
opts
|> Keyword.put_new(:type, "submit")
|> Keyword.put_new(:value, text)
unless to do
raise ArgumentError, "option :to is required in button/2"
end
form_tag(to, form) do
tag(:input, opts)
end
end
defp form_options(opts, method, class) do
{form, opts} = Keyword.pop(opts, :form, [])
form =
form
|> Keyword.put_new(:class, class)
|> Keyword.put_new(:method, method)
|> Keyword.put_new(:enforce_utf8, false)
{form, opts}
end
end