Templates may be defined at the end of the source file. En este ejemplo trabajamos con varios templates inline en diferentes ficheros:
[~/sinatra/sinatraupandrunning/chapter2/views(master)]$ cat example2-14.rb require 'sinatra/base' class App < Sinatra::Base enable :inline_templates get '/index' do puts "Visiting #{request.url}" erb :index end end require './another' __END__ @@index <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Inline template</title> </head> <body> <h1>Worked!</h1> </body> </html>
En este fichero tenemos un segundo template inline:
[~/sinatra/sinatraupandrunning/chapter2/views(master)]$ cat another.rb class App enable :inline_templates get '/' do erb :another end end __END__ @@another <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Separated file</title> </head> <body> <h1>Inside another!</h1> </body> </html>
Este es nuestro config.ru:
[~/sinatra/sinatraupandrunning/chapter2/views(master)]$ cat config.ru require './example2-14' run AppPara simplificar las cosas hemos hecho un
Rakefile
:
[~/sinatra/sinatraupandrunning/chapter2/views(master)]$ cat Rakefile task :default => :server desc "run server" task :server do sh "rackup" end desc "make a get / request via curl" task :root do sh "curl -v localhost:9292" end desc "make a get /index request via curl" task :index do sh "curl -v localhost:9292/index" end
El resultado de la ejecución es:
[~/sinatra/sinatra-up-and-running/chapter2/views/inline_templates(master)]$ curl http://localhost:9292/index <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Inline template</title> </head> <body> <h1>Worked!</h1> </body> </html> [~/sinatra/sinatra-up-and-running/chapter2/views/inline_templates(master)]$ curl http://localhost:9292/ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Separated file</title> </head> <body> <h1>Inside another!</h1> </body> </html>