1.

How Do I Access Helpers From Within My Views?

Answer»

CALL them! Views automatically have access to all helper methods. In fact, Sinatra evaluates routes, views, and helpers WITHIN the same exact object CONTEXT so they all have access to the same methods and instance VARIABLES.

In hello.rb:

helpers do

def em(text)

"<em&GT;#{text}</em>"

end

end

get '/hello' do

@subject = 'World'

haml :hello

end

In views/hello.haml:

%p= "Hello " + em(@subject)

Call them! Views automatically have access to all helper methods. In fact, Sinatra evaluates routes, views, and helpers within the same exact object context so they all have access to the same methods and instance variables.

In hello.rb:

helpers do

def em(text)

"<em>#{text}</em>"

end

end

get '/hello' do

@subject = 'World'

haml :hello

end

In views/hello.haml:

%p= "Hello " + em(@subject)



Discussion

No Comment Found