Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra via rackup does not like inline templates

When calling sinatra itself, $ ruby tubemp.rb works. But via rackup it does not. The application, somehow cannot find the inline templates.

#config.ru
require 'rubygems'
require 'sinatra'

set :environment, ENV['RACK_ENV'].to_sym
disable :run, :reload

require './tubemp.rb'

run Sinatra::Application

The error being returned is:

No such file or directory - /home/ber/Documenten/ET_tubemp/code/views/index.erb:

Relevant part from tubemp.rb

get '/' do
  #...
  erb :index
end

__END__
@@ layout
<html>
...
<%= yield %>

@@ index
Welcome!

Somehow, via rackup, it expects the views to live in actual view-files. I guess the rackup cannot handle the __END__ token when including or so.

How should I deal with this, other then moving my templates into template files?

like image 414
berkes Avatar asked Mar 26 '13 14:03

berkes


People also ask

What is Sinatra :: Base?

Sinatra::Base is the Sinatra without delegation. Consider the following code with delegation: # app.rb require 'sinatra' get '/' do render :template end.

What is Sinatra framework?

Sinatra is a free and open source software web application library and domain-specific language written in Ruby. It is an alternative to other Ruby web application frameworks such as Ruby on Rails, Merb, Nitro, and Camping. It is dependent on the Rack web server interface.


1 Answers

From the docs:

Inline templates defined in the source file that requires sinatra are automatically loaded. Call enable :inline_templates explicitly if you have inline templates in other source files.

In this case, when you use rackup it is your config.ru that calls require 'sinatra', and Sinatra is looking in that file for any templates, and doesn’t find any. When you run your app file directly Sinatra searches tubemp.rb for the templates, and finds them.

To fix it, add

enable :inline_templates

to your tubemp.rb file (and any other source files that have inline templates).

like image 141
matt Avatar answered Dec 27 '22 13:12

matt