The string you're returning from the get '/nesh'
block is exactly what will be returned in the HTTP request, which is why it is not wrapped in <html>...</html>
tags. If you want there to be surrounding tags such as <html>
, <body>
, etc., then you should create a Sinatra view template, and pass your view information (such as name
) into the view renderer.
A complete example might look like this:
app.rb:
set :views, settings.root + '/templates'
get '/nesh' do
name = "Swapnesh"
erb :hello, :locals => {:name => name}
end
templates/layout.erb:
<html>
<body>
<%= yield %>
</body>
</html>
templates/hello.rb:
<h1>Hello <%= name %> Sinha</h1>
Notice I also used a Sinatra layout, which is a base template for all rendered actions (unless opted-out).
Finally, you could implement it all in one file using named templates:
template :layout do
<<-EOF
<html>
<body>
<%= yield %>
</body>
</html>
EOF
end
template :hello do
"<h1>Hello <%= name %> Sinha</h1>"
end
get '/nesh' do
name = "Swapnesh"
erb :hello, :locals => {:name => name}
end