I've done gem install sinatra
and gem install haml
And I have this .rb file
require 'sinatra'
get '/abc2' do
"<b>aaaaaaaaaaaaa</b>"
end
Now say I want that line of HTML but in HAML. and not externally
I know I can do
get '/abc' do
haml :index # /views/index.haml
end
And index.haml could have a line of haml %b aaaaaaa
But is there any way that I can include %b aaaaaaa
in my ruby file itself and have it rendered. Without having to refer to a file e.g. without having to refer to /views/index.haml ?
Like CSS lets you have the CSS External, or it lets you have it internal within the html file.. Similarly, Javascript lets you import javascript externally, or it lets you have it internal to the html file.. Well i'm asking about using HAML internally to the .rb file. Is it possible?
I know HAML is intended as a template language for injecting data into.. but it is also a shorthand for writing HTML more concisely.
You should be able to use Haml::Engine#render
to convert the Haml to HTML like so:
get '/abc2' do
Haml::Engine.new(<<-Haml).render(binding)
%b aaaaaa
Haml
end
This uses a heredoc (everything between the <<-Haml
line and the closing Haml
. binding
is a special variable that basically refers to the current scope.