Module: Hanami::Helpers::LinkToHelper

Includes:
HtmlHelper
Defined in:
lib/hanami/helpers/link_to_helper.rb

Overview

LinkTo Helper

Including Hanami::Helpers::LinkTo will include the link_to public method.

This helper can be used both in views and templates.

Since:

  • 0.2.0

Instance Method Summary collapse

Instance Method Details

Generates an anchor tag for the given arguments.

Contents are automatically escaped.

Examples:

Both content and URL are strings

<%= link_to('Home', '/') %>
  # => <a href="/">Home</a>

Content string with route helper

<%= link_to('Home', routes.path(:home)) %>
  # => <a href="/">Home</a>

HTML attributes

<%= link_to('Home', routes.path(:home), class: 'button') %>
  # => <a href="/" class="button">Home</a>

Automatic content escape (XSS protection)

<%= link_to(%(<script>alert('xss')</script>), '/') %>
  # => <a href="/">&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</a>

Automatic content block escape (XSS protection)

<%=
  link_to('/') do
    %(<script>alert('xss')</script>)
  end
%>
  # => <a href="/">\n&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;\n</a>

Content as block with string URL

<%=
  link_to('/') do
    'Home'
  end
%>
  # => <a href="/">Home</a>

Content as block

<%=
  link_to(routes.path(:home)) do
    'Home'
  end
%>
  # => <a href="/">Home</a>

Content as block with HTML attributes

<%=
  link_to(routes.path(:home), id: 'home-link') do
    'Home'
  end
%>
  # => <a href="/" id: 'home-link'>Home</a>

Content as HTML builder block

<%=
  link_to(routes.path(:home)) do
    strong 'Home'
  end
%>
  # => <a href="/"><strong>Home</strong></a>

Provides both content as first argument and block

<%=
  link_to('Home', routes.path(:home)) do
    strong 'Home'
  end
%>
  # => ArgumentError

Without any argument

<%= link_to %>
  # => ArgumentError

Without any argument and empty block

<%=
  link_to do
  end
%>
  # => ArgumentError

With only content

<%= link_to 'Home' %>
  # => ArgumentError

Overloads:

  • #link_to(content, url, options) ⇒ String

    Use string as content

    Parameters:

    • content (String)

      content used in the a tag

    • url (String)

      url used in href attribute

    • options (Hash)

      HTML attributes to pass to the a tag

  • #link_to(url, options, &blk) ⇒ String

    Use block as content

    Parameters:

    • url (String)

      url used in href attribute

    • options (Hash)

      HTML attributes to pass to the a tag

    • blk (Proc)

      A block that describes the contents of the a tag

Returns:

  • (String)

    HTML markup for the link

Raises:

  • (ArgumentError)

    if the signature isn’t respected

See Also:

  • HtmlHelper#html

Since:

  • 0.2.0



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hanami/helpers/link_to_helper.rb', line 118

def link_to(content, url = nil, options = {}, &blk) # rubocop:disable Metrics/MethodLength
  if block_given?
    options = url || {}
    url     = content
    content = nil
  end

  begin
    options[:href] = url or raise ArgumentError
  rescue TypeError
    raise ArgumentError
  end

  html.a(blk || content, options)
end