Module: Hanami::Helpers::RoutingHelper

Defined in:
lib/hanami/helpers/routing_helper.rb

Overview

Routing helper for full stack Hanami web applications.

For a given application called Web::Application, at runtime Hanami creates a routes factory called Web.routes.

By including this module in a view, it makes that factory avaliable as routes.

Examples:

Basic usage in template

require 'hanami'

module Web::Views::Home
  class Index
    include Web::View
  end
end

# ERB template
# <%= routes.home_path %>

Basic usage in view

require 'hanami'

module Web::Views::Home
  class Index
    include Web::View

    def link_to_home
      %(<a href="#{ routes.home_path }">Home</a>)
    end
  end
end

# ERB template
# <%= link_to_home %>

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Since:

  • 0.1.0


43
44
45
46
47
48
49
50
51
# File 'lib/hanami/helpers/routing_helper.rb', line 43

def self.included(base)
  factory = "#{Utils::String.new(base).namespace}.routes"

  base.class_eval <<-END_EVAL, __FILE__, __LINE__
    def routes
      #{factory}
    end
  END_EVAL
end