How to render a plain HTML file with Sinatra?
Does send_file do what you want?
e.g.
get '/myspecialroute' do
send_file 'special.html'
end
Ruby Sinatra Embed erb Partial External HTML File
Solution: Save the file as HTML
and use this gem for conversion into text
:
https://github.com/soundasleep/html2text_ruby
Works fine if the HTML
is simple enough.
Remaining: Still have the issue as using the HTML
file as a partial
.
Solved:
@text = markdown File.read('views/privacy.md')
So park the source file as a markdown
file, which can translate to HTML
. When I need the email
version, I need to translate to HTML
then to text
using the HTML2text
gem. https://rubygems.org/gems/html2text
Sinatra - Render index.html on every request
Shotgun has its own static server that it will use (to avoid forking when not required). Sinatra will also serve static files if configured to do so, but using the modular style app disables this by default.
The fix is to enable the static server:
module SK
module Routes
class Base < Sinatra::Base
# add this line:
enable :static
include Models
get '/*' do
File.read 'public/index.html'
end
helpers Helpers::API
end
end
end
Now Sinatra will serve static files from the public dir before trying to match the route.
Serving static files with Sinatra
Without any additional configuration, Sinatra will serve assets in public
. For the empty route, you'll want to render the index document.
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
Routes should return a String
which become the HTTP response body. File.read
opens a file, reads the file, closes the file and returns a String
.
Static page routing in Sinatra (Ruby)
Probably a better answer will eventually come, until then this is my shot at it.
If this is not what you want:
get '/' do
redirect '/index.html'
end
You might do something like this:
get '/' do
File.new('public/index.html').readlines
end
I'd go with the first one though, Not sure why you want to avoid that redirect
Explain to me a Sinatra setup for dummy's for a preview of html/css code
About public folder:
The public_folder
is relative to your app file myapp.rb
. If you have a public
folder inside the projectname
folder, this is your public folder. If you have your css, js and image files in another folder, say, includes
under project_name
, then you need to change the line:
# Actually, you need to remove the line above from myapp.rb as it is.
# The line means that the public folder which is used to have css, js and
# image files under '/' and that means that even myapp.rb is visible to everyone.
set :public_folder, '/'
# to:
set :public_folder, File.dirname(__FILE__) + '/includes'
And that will serve up css, js and/or image files from the project_name/includes
folder instead of project_name/public
folder.
Reading the html file:
Reading the html files does not depend on the public folder settings. These need not be inside the public folder.
get '/' do
File.read(File.dirname(__FILE__) + '/index.html')
# This says that the app should read the index.html
# Assuming that both myapp.rb and index.html are in the same folder.
# incase the html files are inside a different directory, say, html,
# change that line to:
# File.read(File.dirname(__FILE__) + '/html/index.html')
# Directory structure sample:
# project_name
# | - myapp.rb
# | - index.html (and not html/index.html etc.)
# | /public (or includes incase the css, js assets have a different location)
# | | /css
# | | /js
# | | /images
end
To get the html output inside the browser
After the file is read, typically, this will be your string: "<html><head></head><body></body></html>"
Without escaping the string, the browser renders the html string as html (no pun) and that's why you won't see any text. To escape the html, you can use the CGI class provided by Ruby (hat tip). So, in the end, this will be your snippet:
get '/' do
CGI::escapeHTML(File.read(File.dirname(__FILE__) + 'index.html'))
end
But that will spit out the html file in a single line. To clean it up,
# myapp.rb
get '/' do
@raw_html = CGI::escapeHTML(File.read(File.dirname(__FILE__) + 'index.html'))
end
# Using inline templates to keep things simple.
# get '/' do...end gets the index.erb file and hence, in the inline template,
# we need to use the @@ index representation. If we say get '/home' do...end,
# then the inline template will come under @@ home. All the html/erb between
# two "@@"s will be rendered as one template (also called as view).
# The <%= @raw_html %>spews out the entire html string read inside the "get" block
__END__
@@ index
<html>
<head></head>
<body>
<pre>
<%= @raw_html %>
</pre>
</body>
</html>
How do I automatically generate static HTML from HAML with Sinatra or Padrino?
Simple,
add padrino-cache to your app
class SimpleApp < Padrino::Application
register Padrino::Cache
enable :caching
get '/foo', :cache => true do
expires_in 30 # expire cached version at least every 30 seconds
'Hello world'
end
end
Then save wherever you want to serve it:
set :cache, Padrino::Cache::Store::File.new(Padrino.root('public'))
You can read more here: http://www.padrinorb.com/guides/padrino-cache
Displaying .txt file with Ruby Sinatra
You can do as below :
sinatra code :
get '/create' do
@logfile = File.open("logfile.txt","r")
erb :create
@logfile.close
end
file.erb
<h2>Text file:</h2>
<% @logfile.each_line do |line| %>
<%= line %>
<% end %>
Or you can use File#read
:
file.erb
<h2>Text file:</h2>
<%= @logfile.read %>
Related Topics
"Errno::Eaccess...Permission Denied" Running Compass Watch
Don't Have Jekyll-Paginate or One of Its Dependencies Installed
Ruby Create Recursive Directory Tree
What Is a Worker in Ruby/Rails
Find Memory Leak in a Ruby on Rails Project
Why Do I Get an "Undefined Method for 'Has_Attached_File' When Installing Paperclip
Convert Array to Hash While Preserving Array Index Values in Ruby
How to Test for a Redirect with Rspec and Capybara
Model Using Modules in Rails Application
Jruby: Import VS Include VS Java_Import VS Include_Class
How to Load Some Activerecord Models from a Yaml File and Save Them to the Db
How to Render a Plain HTML File with Sinatra
Sinatra - Response.Set_Cookie Doesn't Work
How to Get Ruby to Parse Time as If It Were in a Different Time Zone