Skip to content
Fetching latest commit…
Cannot retrieve the latest commit at this time.
..
Failed to load latest commit information.
README.de.html
README.es.html
README.fr.html
README.html
README.hu.html
README.ja.html
README.ko.html
README.pt-br.html
README.pt-pt.html
README.ru.html
README.zh.html
sinatra-config-file.html
sinatra-content-for.html
sinatra-cookies.html
sinatra-decompile.html
sinatra-extension.html
sinatra-json.html
sinatra-link-header.html
sinatra-multi-route.html
sinatra-namespace.html
sinatra-reloader.html
sinatra-respond-with.html
sinatra-streaming.html

README.html

<div class='toc'>
	<ol class='level-1'>
		<li><a href='#Routes'>Routes</a></li>
		<li><a href='#Conditions'>Conditions</a></li>
		<li><a href='#Return%20Values'>Return Values</a></li>
		<li><a href='#Custom%20Route%20Matchers'>Custom Route Matchers</a></li>
		<li><a href='#Static%20Files'>Static Files</a></li>
		<li><a href='#Views%20/%20Templates'>Views / Templates</a></li>
		<ol class='level-2'>
			<li><a href='#Literal%20Templates'>Literal Templates</a></li>
			<li><a href='#Available%20Template%20Languages'>Available Template Languages</a></li>
			<ol class='level-3'>
				<li><a href='#Haml%20Templates'>Haml Templates</a></li>
				<li><a href='#Erb%20Templates'>Erb Templates</a></li>
				<li><a href='#Builder%20Templates'>Builder Templates</a></li>
				<li><a href='#Nokogiri%20Templates'>Nokogiri Templates</a></li>
				<li><a href='#Sass%20Templates'>Sass Templates</a></li>
				<li><a href='#SCSS%20Templates'>SCSS Templates</a></li>
				<li><a href='#Less%20Templates'>Less Templates</a></li>
				<li><a href='#Liquid%20Templates'>Liquid Templates</a></li>
				<li><a href='#Markdown%20Templates'>Markdown Templates</a></li>
				<li><a href='#Textile%20Templates'>Textile Templates</a></li>
				<li><a href='#RDoc%20Templates'>RDoc Templates</a></li>
				<li><a href='#AsciiDoc%20Templates'>AsciiDoc Templates</a></li>
				<li><a href='#Radius%20Templates'>Radius Templates</a></li>
				<li><a href='#Markaby%20Templates'>Markaby Templates</a></li>
				<li><a href='#RABL%20Templates'>RABL Templates</a></li>
				<li><a href='#Slim%20Templates'>Slim Templates</a></li>
				<li><a href='#Creole%20Templates'>Creole Templates</a></li>
				<li><a href='#MediaWiki%20Templates'>MediaWiki Templates</a></li>
				<li><a href='#CoffeeScript%20Templates'>CoffeeScript Templates</a></li>
				<li><a href='#Stylus%20Templates'>Stylus Templates</a></li>
				<li><a href='#Yajl%20Templates'>Yajl Templates</a></li>
				<li><a href='#WLang%20Templates'>WLang Templates</a></li>
			</ol>
			<li><a href='#Accessing%20Variables%20in%20Templates'>Accessing Variables in Templates</a></li>
			<li><a href='#Templates%20with%20%3Ccode%3Eyield%3C/code%3E%20and%20nested%20layouts'>Templates with <code>yield</code> and nested layouts</a></li>
			<li><a href='#Inline%20Templates'>Inline Templates</a></li>
			<li><a href='#Named%20Templates'>Named Templates</a></li>
			<li><a href='#Associating%20File%20Extensions'>Associating File Extensions</a></li>
			<li><a href='#Adding%20Your%20Own%20Template%20Engine'>Adding Your Own Template Engine</a></li>
			<li><a href='#Using%20Custom%20Logic%20for%20Template%20Lookup'>Using Custom Logic for Template Lookup</a></li>
		</ol>
		<li><a href='#Filters'>Filters</a></li>
		<li><a href='#Helpers'>Helpers</a></li>
		<ol class='level-2'>
			<li><a href='#Using%20Sessions'>Using Sessions</a></li>
			<li><a href='#Halting'>Halting</a></li>
			<li><a href='#Passing'>Passing</a></li>
			<li><a href='#Triggering%20Another%20Route'>Triggering Another Route</a></li>
			<li><a href='#Setting%20Body,%20Status%20Code%20and%20Headers'>Setting Body, Status Code and Headers</a></li>
			<li><a href='#Streaming%20Responses'>Streaming Responses</a></li>
			<li><a href='#Logging'>Logging</a></li>
			<li><a href='#Mime%20Types'>Mime Types</a></li>
			<li><a href='#Generating%20URLs'>Generating URLs</a></li>
			<li><a href='#Browser%20Redirect'>Browser Redirect</a></li>
			<li><a href='#Cache%20Control'>Cache Control</a></li>
			<li><a href='#Sending%20Files'>Sending Files</a></li>
			<li><a href='#Accessing%20the%20Request%20Object'>Accessing the Request Object</a></li>
			<li><a href='#Attachments'>Attachments</a></li>
			<li><a href='#Dealing%20with%20Date%20and%20Time'>Dealing with Date and Time</a></li>
			<li><a href='#Looking%20Up%20Template%20Files'>Looking Up Template Files</a></li>
		</ol>
		<li><a href='#Configuration'>Configuration</a></li>
		<ol class='level-2'>
			<li><a href='#Configuring%20attack%20protection'>Configuring attack protection</a></li>
			<li><a href='#Available%20Settings'>Available Settings</a></li>
		</ol>
		<li><a href='#Environments'>Environments</a></li>
		<li><a href='#Error%20Handling'>Error Handling</a></li>
		<ol class='level-2'>
			<li><a href='#Not%20Found'>Not Found</a></li>
			<li><a href='#Error'>Error</a></li>
		</ol>
		<li><a href='#Rack%20Middleware'>Rack Middleware</a></li>
		<li><a href='#Testing'>Testing</a></li>
		<li><a href='#Sinatra::Base%20-%20Middleware,%20Libraries,%20and%20Modular%20Apps'>Sinatra::Base - Middleware, Libraries, and Modular Apps</a></li>
		<ol class='level-2'>
			<li><a href='#Modular%20vs.%20Classic%20Style'>Modular vs. Classic Style</a></li>
			<li><a href='#Serving%20a%20Modular%20Application'>Serving a Modular Application</a></li>
			<li><a href='#Using%20a%20Classic%20Style%20Application%20with%20a%20config.ru'>Using a Classic Style Application with a config.ru</a></li>
			<li><a href='#When%20to%20use%20a%20config.ru?'>When to use a config.ru?</a></li>
			<li><a href='#Using%20Sinatra%20as%20Middleware'>Using Sinatra as Middleware</a></li>
			<li><a href='#Dynamic%20Application%20Creation'>Dynamic Application Creation</a></li>
		</ol>
		<li><a href='#Scopes%20and%20Binding'>Scopes and Binding</a></li>
		<ol class='level-2'>
			<li><a href='#Application/Class%20Scope'>Application/Class Scope</a></li>
			<li><a href='#Request/Instance%20Scope'>Request/Instance Scope</a></li>
			<li><a href='#Delegation%20Scope'>Delegation Scope</a></li>
		</ol>
		<li><a href='#Command%20Line'>Command Line</a></li>
		<ol class='level-2'>
			<li><a href='#Multi-threading'>Multi-threading</a></li>
		</ol>
		<li><a href='#Requirement'>Requirement</a></li>
		<li><a href='#The%20Bleeding%20Edge'>The Bleeding Edge</a></li>
		<ol class='level-2'>
			<li><a href='#With%20Bundler'>With Bundler</a></li>
			<li><a href='#Roll%20Your%20Own'>Roll Your Own</a></li>
			<li><a href='#Install%20Globally'>Install Globally</a></li>
		</ol>
		<li><a href='#Versioning'>Versioning</a></li>
		<li><a href='#Further%20Reading'>Further Reading</a></li>
	</ol>
</div>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>

<p>Sinatra is a <a href="https://en.wikipedia.org/wiki/Domain-specific_language">DSL</a> for
quickly creating web applications in Ruby with minimal effort:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># myapp.rb</span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello world!</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Install the gem:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>gem install sinatra
</pre></div>
</div>
</div>

<p>And run with:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>ruby myapp.rb
</pre></div>
</div>
</div>

<p>View at: http://localhost:4567</p>

<p>It is recommended to also run <code>gem install thin</code>, which Sinatra will
pick up if available.</p>





<a name='Routes'></a>
<h2>Routes</h2>

<p>In Sinatra, a route is an HTTP method paired with a URL-matching pattern.
Each route is associated with a block:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. show something ..
<span style="color:#080;font-weight:bold">end</span>

post <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. create something ..
<span style="color:#080;font-weight:bold">end</span>

put <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. replace something ..
<span style="color:#080;font-weight:bold">end</span>

patch <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. modify something ..
<span style="color:#080;font-weight:bold">end</span>

delete <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. annihilate something ..
<span style="color:#080;font-weight:bold">end</span>

options <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. appease something ..
<span style="color:#080;font-weight:bold">end</span>

link <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. affiliate something ..
<span style="color:#080;font-weight:bold">end</span>

unlink <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  .. separate something ..
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Routes are matched in the order they are defined. The first route that
matches the request is invoked.</p>

<p>Route patterns may include named parameters, accessible via the
<code>params</code> hash:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/hello/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># matches "GET /hello/foo" and "GET /hello/bar"</span>
  <span style="color:#777"># params['name'] is 'foo' or 'bar'</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">name</span><span style="color:#710">'</span></span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You can also access named parameters via block parameters:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/hello/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span> |n|
  <span style="color:#777"># matches "GET /hello/foo" and "GET /hello/bar"</span>
  <span style="color:#777"># params['name'] is 'foo' or 'bar'</span>
  <span style="color:#777"># n stores params['name']</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>n<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Route patterns may also include splat (or wildcard) parameters, accessible
via the <code>params['splat']</code> array:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/say/*/to/*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># matches /say/hello/to/world</span>
  params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">splat</span><span style="color:#710">'</span></span>] <span style="color:#777"># =&gt; ["hello", "world"]</span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/download/*.*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># matches /download/path/to/file.xml</span>
  params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">splat</span><span style="color:#710">'</span></span>] <span style="color:#777"># =&gt; ["path/to/file", "xml"]</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Or with block parameters:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/download/*.*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span> |path, ext|
  [path, ext] <span style="color:#777"># =&gt; ["path/to/file", "xml"]</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Route matching with Regular Expressions:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\A</span><span style="color:#D20">\/</span><span style="color:#808">hello</span><span style="color:#D20">\/</span><span style="color:#808">([</span><span style="color:#D20">\w</span><span style="color:#808">]+)</span><span style="color:#D20">\z</span><span style="color:#404">/</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">captures</span><span style="color:#710">'</span></span>].first<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Or with a block parameter:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">%r{</span><span style="color:#808">/hello/([</span><span style="color:#D20">\w</span><span style="color:#808">]+)</span><span style="color:#404">}</span></span> <span style="color:#080;font-weight:bold">do</span> |c|
  <span style="color:#777"># Matches "GET /meta/hello/world", "GET /hello/world/1234" etc.</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>c<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Route patterns may have optional parameters:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/posts.?:format?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># matches "GET /posts" and any extension "GET /posts.json", "GET /posts.xml" etc.</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Routes may also utilize query parameters:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/posts</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># matches "GET /posts?title=foo&amp;author=bar"</span>
  title = params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">title</span><span style="color:#710">'</span></span>]
  author = params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">author</span><span style="color:#710">'</span></span>]
  <span style="color:#777"># uses title and author variables; query is optional to the /posts route</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>By the way, unless you disable the path traversal attack protection (see below),
the request path might be modified before matching against your routes.</p>

<a name='Conditions'></a>
<h2>Conditions</h2>

<p>Routes may include a variety of matching conditions, such as the user agent:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span>, <span style="color:#A60">:agent</span> =&gt; <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Songbird (</span><span style="color:#D20">\d</span><span style="color:#D20">\.</span><span style="color:#D20">\d</span><span style="color:#808">)[</span><span style="color:#D20">\d</span><span style="color:#D20">\/</span><span style="color:#808">]*?</span><span style="color:#404">/</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">You're using Songbird version </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">agent</span><span style="color:#710">'</span></span>][<span style="color:#00D">0</span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># Matches non-songbird browsers</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Other available conditions are <code>host_name</code> and <code>provides</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>, <span style="color:#A60">:host_name</span> =&gt; <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">^admin</span><span style="color:#D20">\.</span><span style="color:#404">/</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Admin Area, Access denied!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>, <span style="color:#A60">:provides</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">html</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>, <span style="color:#A60">:provides</span> =&gt; [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rss</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">atom</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">xml</span><span style="color:#710">'</span></span>] <span style="color:#080;font-weight:bold">do</span>
  builder <span style="color:#A60">:feed</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>
<p><code>provides</code> searches the request’s Accept header.</p>

<p>You can easily define your own conditions:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set(<span style="color:#A60">:probability</span>) { |value| condition { rand &lt;= value } }

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/win_a_car</span><span style="color:#710">'</span></span>, <span style="color:#A60">:probability</span> =&gt; <span style="color:#60E">0.1</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">You won!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/win_a_car</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Sorry, you lost.</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>For a condition that takes multiple values use a splat:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set(<span style="color:#A60">:auth</span>) <span style="color:#080;font-weight:bold">do</span> |*roles|   <span style="color:#777"># &lt;- notice the splat here</span>
  condition <span style="color:#080;font-weight:bold">do</span>
    <span style="color:#080;font-weight:bold">unless</span> logged_in? &amp;&amp; roles.any? {|role| current_user.in_role? role }
      redirect <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/login/</span><span style="color:#710">"</span></span>, <span style="color:#00D">303</span>
    <span style="color:#080;font-weight:bold">end</span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/my/account/</span><span style="color:#710">"</span></span>, <span style="color:#A60">:auth</span> =&gt; [<span style="color:#A60">:user</span>, <span style="color:#A60">:admin</span>] <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Your Account Details</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/only/admin/</span><span style="color:#710">"</span></span>, <span style="color:#A60">:auth</span> =&gt; <span style="color:#A60">:admin</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Only admins are allowed here!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Return%20Values'></a>
<h2>Return Values</h2>

<p>The return value of a route block determines at least the response body passed
on to the HTTP client, or at least the next middleware in the Rack stack.
Most commonly, this is a string, as in the above examples. But other values are
also accepted.</p>

<p>You can return any object that would either be a valid Rack response, Rack
body object or HTTP status code:</p>

<ul>
  <li>An Array with three elements: <code>[status (Fixnum), headers (Hash), response
body (responds to #each)]</code>
</li>
  <li>An Array with two elements: <code>[status (Fixnum), response body (responds to
#each)]</code>
</li>
  <li>An object that responds to <code>#each</code> and passes nothing but strings to
the given block</li>
  <li>A Fixnum representing the status code</li>
</ul>

<p>That way we can, for instance, easily implement a streaming example:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Stream</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">each</span>
    <span style="color:#00D">100</span>.times { |i| <span style="color:#080;font-weight:bold">yield</span> <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>i<span style="font-weight:bold;color:#666">}</span></span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span> }
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { <span style="color:#036;font-weight:bold">Stream</span>.new }
</pre></div>
</div>
</div>

<p>You can also use the <code>stream</code> helper method (described below) to reduce boiler
plate and embed the streaming logic in the route.</p>

<a name='Custom%20Route%20Matchers'></a>
<h2>Custom Route Matchers</h2>

<p>As shown above, Sinatra ships with built-in support for using String patterns
and regular expressions as route matches. However, it does not stop there. You
can easily define your own matchers:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">AllButPattern</span>
  <span style="color:#036;font-weight:bold">Match</span> = <span style="color:#036;font-weight:bold">Struct</span>.new(<span style="color:#A60">:captures</span>)

  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">initialize</span>(except)
    <span style="color:#33B">@except</span>   = except
    <span style="color:#33B">@captures</span> = <span style="color:#036;font-weight:bold">Match</span>.new([])
  <span style="color:#080;font-weight:bold">end</span>

  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">match</span>(str)
    <span style="color:#33B">@captures</span> <span style="color:#080;font-weight:bold">unless</span> <span style="color:#33B">@except</span> === str
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

<span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">all_but</span>(pattern)
  <span style="color:#036;font-weight:bold">AllButPattern</span>.new(pattern)
<span style="color:#080;font-weight:bold">end</span>

get all_but(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/index</span><span style="color:#710">"</span></span>) <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># ...</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Note that the above example might be over-engineered, as it can also be
expressed as:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#404">/</span></span> <span style="color:#080;font-weight:bold">do</span>
  pass <span style="color:#080;font-weight:bold">if</span> request.path_info == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/index</span><span style="color:#710">"</span></span>
  <span style="color:#777"># ...</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Or, using negative look ahead:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">%r{</span><span style="color:#808">^(?!/index$)</span><span style="color:#404">}</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># ...</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Static%20Files'></a>
<h2>Static Files</h2>

<p>Static files are served from the <code>./public</code> directory. You can specify
a different location by setting the <code>:public_folder</code> option:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:public_folder</span>, <span style="color:#036;font-weight:bold">File</span>.dirname(<span style="color:#069">__FILE__</span>) + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/static</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>Note that the public directory name is not included in the URL. A file
<code>./public/css/style.css</code> is made available as
<code>http://example.com/css/style.css</code>.</p>

<p>Use the <code>:static_cache_control</code> setting (see below) to add
<code>Cache-Control</code> header info.</p>

<a name='Views%20/%20Templates'></a>
<h2>Views / Templates</h2>

<p>Each template language is exposed via its own rendering method. These
methods simply return a string:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  erb <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This renders <code>views/index.erb</code>.</p>

<p>Instead of a template name, you can also just pass in the template content
directly:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  code = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">&lt;%= Time.now %&gt;</span><span style="color:#710">"</span></span>
  erb code
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Templates take a second argument, the options hash:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  erb <span style="color:#A60">:index</span>, <span style="color:#A60">:layout</span> =&gt; <span style="color:#A60">:post</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This will render <code>views/index.erb</code> embedded in the
<code>views/post.erb</code> (default is <code>views/layout.erb</code>, if it exists).</p>

<p>Any options not understood by Sinatra will be passed on to the template
engine:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="color:#A60">:index</span>, <span style="color:#A60">:format</span> =&gt; <span style="color:#A60">:html5</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You can also set options per template language in general:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:haml</span>, <span style="color:#A60">:format</span> =&gt; <span style="color:#A60">:html5</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Options passed to the render method override options set via <code>set</code>.</p>

<p>Available Options:</p>

<dl>
  <dt>locals</dt>
  <dd>
    List of locals passed to the document. Handy with partials.
    Example: <tt>erb "&lt;%= foo %&gt;", :locals =&gt; {:foo =&gt; "bar"}</tt>
  </dd>

  <dt>default_encoding</dt>
  <dd>
    String encoding to use if uncertain. Defaults to
    <tt>settings.default_encoding</tt>.
  </dd>

  <dt>views</dt>
  <dd>
    Views folder to load templates from. Defaults to <tt>settings.views</tt>.
  </dd>

  <dt>layout</dt>
  <dd>
    Whether to use a layout (<tt>true</tt> or <tt>false</tt>). If it's a
    Symbol, specifies what template to use. Example:
    <tt>erb :index, :layout =&gt; !request.xhr?</tt>
  </dd>

  <dt>content_type</dt>
  <dd>
    Content-Type the template produces. Default depends on template language.
  </dd>

  <dt>scope</dt>
  <dd>
    Scope to render template under. Defaults to the application instance. If you
    change this, instance variables and helper methods will not be available.
  </dd>

  <dt>layout_engine</dt>
  <dd>
    Template engine to use for rendering the layout. Useful for languages that
    do not support layouts otherwise. Defaults to the engine used for the
    template. Example: <tt>set :rdoc, :layout_engine =&gt; :erb</tt>
  </dd>

  <dt>layout_options</dt>
  <dd>
    Special options only used for rendering the layout. Example:
    <tt>set :rdoc, :layout_options =&gt; { :views =&gt; 'views/layouts' }</tt>
  </dd>
</dl>

<p>Templates are assumed to be located directly under the <code>./views</code> directory. To
use a different views directory:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:views</span>, settings.root + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/templates</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>One important thing to remember is that you always have to reference templates
with symbols, even if they’re in a subdirectory (in this case, use:
<code>:'subdir/template'</code> or <code>'subdir/template'.to_sym</code>). You must use a symbol
because otherwise rendering methods will render any strings passed to them
directly.</p>

<a name='Literal%20Templates'></a>
<h3>Literal Templates</h3>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%div.title Hello World</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Renders the template string.</p>

<a name='Available%20Template%20Languages'></a>
<h3>Available Template Languages</h3>

<p>Some languages have multiple implementations. To specify what implementation
to use (and to be thread-safe), you should simply require it first:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rdiscount</span><span style="color:#710">'</span></span> <span style="color:#777"># or require 'bluecloth'</span>
get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { markdown <span style="color:#A60">:index</span> }
</pre></div>
</div>
</div>

<a name='Haml%20Templates'></a>
<h4>Haml Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://haml.info/" title="haml">haml</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.haml</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>haml :index, :format =&gt; :html5</tt></td>
  </tr>
</table>

<a name='Erb%20Templates'></a>
<h4>Erb Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td>
      <a href="http://www.kuwata-lab.com/erubis/" title="erubis">erubis</a>
      or erb (included in Ruby)
    </td>
  </tr>
  <tr>
    <td>File Extensions</td>
    <td>
<tt>.erb</tt>, <tt>.rhtml</tt> or <tt>.erubis</tt> (Erubis only)</td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>erb :index</tt></td>
  </tr>
</table>

<a name='Builder%20Templates'></a>
<h4>Builder Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td>
      <a href="https://github.com/jimweirich/builder" title="builder">builder</a>
    </td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.builder</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>builder { |xml| xml.em "hi" }</tt></td>
  </tr>
</table>

<p>It also takes a block for inline templates (see example).</p>

<a name='Nokogiri%20Templates'></a>
<h4>Nokogiri Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://www.nokogiri.org/" title="nokogiri">nokogiri</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.nokogiri</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>nokogiri { |xml| xml.em "hi" }</tt></td>
  </tr>
</table>

<p>It also takes a block for inline templates (see example).</p>

<a name='Sass%20Templates'></a>
<h4>Sass Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://sass-lang.com/" title="sass">sass</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.sass</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>sass :stylesheet, :style =&gt; :expanded</tt></td>
  </tr>
</table>

<a name='SCSS%20Templates'></a>
<h4>SCSS Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://sass-lang.com/" title="sass">sass</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.scss</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>scss :stylesheet, :style =&gt; :expanded</tt></td>
  </tr>
</table>

<a name='Less%20Templates'></a>
<h4>Less Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://lesscss.org/" title="less">less</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.less</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>less :stylesheet</tt></td>
  </tr>
</table>

<a name='Liquid%20Templates'></a>
<h4>Liquid Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://liquidmarkup.org/" title="liquid">liquid</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.liquid</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>liquid :index, :locals =&gt; { :key =&gt; 'value' }</tt></td>
  </tr>
</table>

<p>Since you cannot call Ruby methods (except for <code>yield</code>) from a Liquid
template, you almost always want to pass locals to it.</p>

<a name='Markdown%20Templates'></a>
<h4>Markdown Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td>
      Anyone of:
        <a href="https://github.com/davidfstr/rdiscount" title="RDiscount">RDiscount</a>,
        <a href="https://github.com/vmg/redcarpet" title="RedCarpet">RedCarpet</a>,
        <a href="http://deveiate.org/projects/BlueCloth" title="BlueCloth">BlueCloth</a>,
        <a href="http://kramdown.gettalong.org/" title="kramdown">kramdown</a>,
        <a href="https://github.com/bhollis/maruku" title="maruku">maruku</a>
    </td>
  </tr>
  <tr>
    <td>File Extensions</td>
    <td>
<tt>.markdown</tt>, <tt>.mkd</tt> and <tt>.md</tt>
</td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>markdown :index, :layout_engine =&gt; :erb</tt></td>
  </tr>
</table>

<p>It is not possible to call methods from markdown, nor to pass locals to it.
You therefore will usually use it in combination with another rendering
engine:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:overview</span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:text</span> =&gt; markdown(<span style="color:#A60">:introduction</span>) }
</pre></div>
</div>
</div>

<p>Note that you may also call the <code>markdown</code> method from within other templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>%h1 <span style="color:#036;font-weight:bold">Hello</span> <span style="color:#036;font-weight:bold">From</span> Haml!
%p= markdown(<span style="color:#A60">:greetings</span>)
</pre></div>
</div>
</div>

<p>Since you cannot call Ruby from Markdown, you cannot use layouts written in
Markdown. However, it is possible to use another rendering engine for the
template than for the layout by passing the <code>:layout_engine</code> option.</p>

<a name='Textile%20Templates'></a>
<h4>Textile Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://redcloth.org/" title="RedCloth">RedCloth</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.textile</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>textile :index, :layout_engine =&gt; :erb</tt></td>
  </tr>
</table>

<p>It is not possible to call methods from textile, nor to pass locals to it. You
therefore will usually use it in combination with another rendering engine:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:overview</span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:text</span> =&gt; textile(<span style="color:#A60">:introduction</span>) }
</pre></div>
</div>
</div>

<p>Note that you may also call the <code>textile</code> method from within other templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>%h1 <span style="color:#036;font-weight:bold">Hello</span> <span style="color:#036;font-weight:bold">From</span> Haml!
%p= textile(<span style="color:#A60">:greetings</span>)
</pre></div>
</div>
</div>

<p>Since you cannot call Ruby from Textile, you cannot use layouts written in
Textile. However, it is possible to use another rendering engine for the
template than for the layout by passing the <code>:layout_engine</code> option.</p>

<a name='RDoc%20Templates'></a>
<h4>RDoc Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://rdoc.sourceforge.net/" title="RDoc">RDoc</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.rdoc</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>rdoc :README, :layout_engine =&gt; :erb</tt></td>
  </tr>
</table>

<p>It is not possible to call methods from rdoc, nor to pass locals to it. You
therefore will usually use it in combination with another rendering engine:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:overview</span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:text</span> =&gt; rdoc(<span style="color:#A60">:introduction</span>) }
</pre></div>
</div>
</div>

<p>Note that you may also call the <code>rdoc</code> method from within other templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>%h1 <span style="color:#036;font-weight:bold">Hello</span> <span style="color:#036;font-weight:bold">From</span> Haml!
%p= rdoc(<span style="color:#A60">:greetings</span>)
</pre></div>
</div>
</div>

<p>Since you cannot call Ruby from RDoc, you cannot use layouts written in
RDoc. However, it is possible to use another rendering engine for the
template than for the layout by passing the <code>:layout_engine</code> option.</p>

<a name='AsciiDoc%20Templates'></a>
<h4>AsciiDoc Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://asciidoctor.org/" title="Asciidoctor">Asciidoctor</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td>
<tt>.asciidoc</tt>, <tt>.adoc</tt> and <tt>.ad</tt>
</td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>asciidoc :README, :layout_engine =&gt; :erb</tt></td>
  </tr>
</table>

<p>Since you cannot call Ruby methods directly from an AsciiDoc template, you
almost always want to pass locals to it.</p>

<a name='Radius%20Templates'></a>
<h4>Radius Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="https://github.com/jlong/radius" title="Radius">Radius</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.radius</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>radius :index, :locals =&gt; { :key =&gt; 'value' }</tt></td>
  </tr>
</table>

<p>Since you cannot call Ruby methods directly from a Radius template, you almost
always want to pass locals to it.</p>

<a name='Markaby%20Templates'></a>
<h4>Markaby Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://markaby.github.io/" title="Markaby">Markaby</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.mab</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>markaby { h1 "Welcome!" }</tt></td>
  </tr>
</table>

<p>It also takes a block for inline templates (see example).</p>

<a name='RABL%20Templates'></a>
<h4>RABL Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="https://github.com/nesquena/rabl" title="Rabl">Rabl</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.rabl</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>rabl :index</tt></td>
  </tr>
</table>

<a name='Slim%20Templates'></a>
<h4>Slim Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="http://slim-lang.com/" title="Slim Lang">Slim Lang</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.slim</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>slim :index</tt></td>
  </tr>
</table>

<a name='Creole%20Templates'></a>
<h4>Creole Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="https://github.com/minad/creole" title="Creole">Creole</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.creole</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>creole :wiki, :layout_engine =&gt; :erb</tt></td>
  </tr>
</table>

<p>It is not possible to call methods from creole, nor to pass locals to it. You
therefore will usually use it in combination with another rendering engine:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:overview</span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:text</span> =&gt; creole(<span style="color:#A60">:introduction</span>) }
</pre></div>
</div>
</div>

<p>Note that you may also call the <code>creole</code> method from within other templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>%h1 <span style="color:#036;font-weight:bold">Hello</span> <span style="color:#036;font-weight:bold">From</span> Haml!
%p= creole(<span style="color:#A60">:greetings</span>)
</pre></div>
</div>
</div>

<p>Since you cannot call Ruby from Creole, you cannot use layouts written in
Creole. However, it is possible to use another rendering engine for the
template than for the layout by passing the <code>:layout_engine</code> option.</p>

<a name='MediaWiki%20Templates'></a>
<h4>MediaWiki Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="https://github.com/nricciar/wikicloth" title="WikiCloth">WikiCloth</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td>
<tt>.mediawiki</tt> and <tt>.mw</tt>
</td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>mediawiki :wiki, :layout_engine =&gt; :erb</tt></td>
  </tr>
</table>

<p>It is not possible to call methods from MediaWiki markup, nor to pass locals to
it. You therefore will usually use it in combination with another rendering
engine:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:overview</span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:text</span> =&gt; mediawiki(<span style="color:#A60">:introduction</span>) }
</pre></div>
</div>
</div>

<p>Note that you may also call the <code>mediawiki</code> method from within other templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>%h1 <span style="color:#036;font-weight:bold">Hello</span> <span style="color:#036;font-weight:bold">From</span> Haml!
%p= mediawiki(<span style="color:#A60">:greetings</span>)
</pre></div>
</div>
</div>

<p>Since you cannot call Ruby from MediaWiki, you cannot use layouts written in
MediaWiki. However, it is possible to use another rendering engine for the
template than for the layout by passing the <code>:layout_engine</code> option.</p>

<a name='CoffeeScript%20Templates'></a>
<h4>CoffeeScript Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td>
      <a href="https://github.com/josh/ruby-coffee-script" title="Ruby CoffeeScript">
        CoffeeScript
      </a> and a
      <a href="https://github.com/sstephenson/execjs/blob/master/README.md#readme" title="ExecJS">
        way to execute javascript
      </a>
    </td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.coffee</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>coffee :index</tt></td>
  </tr>
</table>

<a name='Stylus%20Templates'></a>
<h4>Stylus Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td>
      <a href="https://github.com/forgecrafted/ruby-stylus" title="Ruby Stylus">
        Stylus
      </a> and a
      <a href="https://github.com/sstephenson/execjs/blob/master/README.md#readme" title="ExecJS">
        way to execute javascript
      </a>
    </td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.styl</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>stylus :index</tt></td>
  </tr>
</table>

<p>Before being able to use Stylus templates, you need to load <code>stylus</code> and
<code>stylus/tilt</code> first:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">stylus</span><span style="color:#710">'</span></span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">stylus/tilt</span><span style="color:#710">'</span></span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  stylus <span style="color:#A60">:example</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Yajl%20Templates'></a>
<h4>Yajl Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="https://github.com/brianmario/yajl-ruby" title="yajl-ruby">yajl-ruby</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.yajl</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td>
      <tt>
        yajl :index,
             :locals =&gt; { :key =&gt; 'qux' },
             :callback =&gt; 'present',
             :variable =&gt; 'resource'
      </tt>
    </td>
  </tr>
</table>

<p>The template source is evaluated as a Ruby string, and the
resulting json variable is converted using <code>#to_json</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>json = { <span style="color:#A60">:foo</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">bar</span><span style="color:#710">'</span></span> }
json[<span style="color:#A60">:baz</span>] = key
</pre></div>
</div>
</div>

<p>The <code>:callback</code> and <code>:variable</code> options can be used to decorate the rendered
object:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">var</span> resource = {<span style="color:#606"><span style="color:#404">"</span><span>foo</span><span style="color:#404">"</span></span>:<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">bar</span><span style="color:#710">"</span></span>,<span style="color:#606"><span style="color:#404">"</span><span>baz</span><span style="color:#404">"</span></span>:<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">qux</span><span style="color:#710">"</span></span>};
present(resource);
</pre></div>
</div>
</div>

<a name='WLang%20Templates'></a>
<h4>WLang Templates</h4>

<table>
  <tr>
    <td>Dependency</td>
    <td><a href="https://github.com/blambeau/wlang/" title="WLang">WLang</a></td>
  </tr>
  <tr>
    <td>File Extension</td>
    <td><tt>.wlang</tt></td>
  </tr>
  <tr>
    <td>Example</td>
    <td><tt>wlang :index, :locals =&gt; { :key =&gt; 'value' }</tt></td>
  </tr>
</table>

<p>Since calling ruby methods is not idiomatic in WLang, you almost always want to
pass locals to it. Layouts written in WLang and <code>yield</code> are supported, though.</p>

<a name='Accessing%20Variables%20in%20Templates'></a>
<h3>Accessing Variables in Templates</h3>

<p>Templates are evaluated within the same context as route handlers. Instance
variables set in route handlers are directly accessible by templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:id</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#33B">@foo</span> = <span style="color:#036;font-weight:bold">Foo</span>.find(params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">id</span><span style="color:#710">'</span></span>])
  haml <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%h1= @foo.name</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Or, specify an explicit Hash of local variables:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:id</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  foo = <span style="color:#036;font-weight:bold">Foo</span>.find(params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">id</span><span style="color:#710">'</span></span>])
  haml <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%h1= bar.name</span><span style="color:#710">'</span></span>, <span style="color:#A60">:locals</span> =&gt; { <span style="color:#A60">:bar</span> =&gt; foo }
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This is typically used when rendering templates as partials from within
other templates.</p>

<a name='Templates%20with%20%3Ccode%3Eyield%3C/code%3E%20and%20nested%20layouts'></a>
<h3>Templates with <code>yield</code> and nested layouts</h3>

<p>A layout is usually just a template that calls <code>yield</code>.
Such a template can be used either through the <code>:template</code> option as
described above, or it can be rendered with a block as follows:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:post</span>, <span style="color:#A60">:layout</span> =&gt; <span style="color:#069">false</span> <span style="color:#080;font-weight:bold">do</span>
  erb <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This code is mostly equivalent to <code>erb :index, :layout =&gt; :post</code>.</p>

<p>Passing blocks to rendering methods is most useful for creating nested layouts:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:main_layout</span>, <span style="color:#A60">:layout</span> =&gt; <span style="color:#069">false</span> <span style="color:#080;font-weight:bold">do</span>
  erb <span style="color:#A60">:admin_layout</span> <span style="color:#080;font-weight:bold">do</span>
    erb <span style="color:#A60">:user</span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This can also be done in fewer lines of code with:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>erb <span style="color:#A60">:admin_layout</span>, <span style="color:#A60">:layout</span> =&gt; <span style="color:#A60">:main_layout</span> <span style="color:#080;font-weight:bold">do</span>
  erb <span style="color:#A60">:user</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Currently, the following rendering methods accept a block: <code>erb</code>, <code>haml</code>,
<code>liquid</code>, <code>slim </code>, <code>wlang</code>. Also the general <code>render</code> method accepts a block.</p>

<a name='Inline%20Templates'></a>
<h3>Inline Templates</h3>

<p>Templates may be defined at the end of the source file:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>

<span style="color:#777">__END__

@@ layout
%html
  = yield

@@ index
%div.title Hello world.</span>
</pre></div>
</div>
</div>

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

<a name='Named%20Templates'></a>
<h3>Named Templates</h3>

<p>Templates may also be defined using the top-level <code>template</code> method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>template <span style="color:#A60">:layout</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">%html</span><span style="color:#b0b">\n</span><span style="color:#D20">  =yield</span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

template <span style="color:#A60">:index</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">%div.title Hello World!</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>If a template named “layout” exists, it will be used each time a template
is rendered. You can individually disable layouts by passing
<code>:layout =&gt; false</code> or disable them by default via
<code>set :haml, :layout =&gt; false</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  haml <span style="color:#A60">:index</span>, <span style="color:#A60">:layout</span> =&gt; !request.xhr?
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Associating%20File%20Extensions'></a>
<h3>Associating File Extensions</h3>

<p>To associate a file extension with a template engine, use
<code>Tilt.register</code>. For instance, if you like to use the file extension
<code>tt</code> for Textile templates, you can do the following:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#036;font-weight:bold">Tilt</span>.register <span style="color:#A60">:tt</span>, <span style="color:#036;font-weight:bold">Tilt</span>[<span style="color:#A60">:textile</span>]
</pre></div>
</div>
</div>

<a name='Adding%20Your%20Own%20Template%20Engine'></a>
<h3>Adding Your Own Template Engine</h3>

<p>First, register your engine with Tilt, then create a rendering method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#036;font-weight:bold">Tilt</span>.register <span style="color:#A60">:myat</span>, <span style="color:#036;font-weight:bold">MyAwesomeTemplateEngine</span>

helpers <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">myat</span>(*args) render(<span style="color:#A60">:myat</span>, *args) <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  myat <span style="color:#A60">:index</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Renders <code>./views/index.myat</code>. See https://github.com/rtomayko/tilt to
learn more about Tilt.</p>

<a name='Using%20Custom%20Logic%20for%20Template%20Lookup'></a>
<h3>Using Custom Logic for Template Lookup</h3>

<p>To implement your own template lookup mechanism you can write your
own <code>#find_template</code> method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>configure <span style="color:#080;font-weight:bold">do</span>
  set <span style="color:#A60">:views</span> [ <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">./views/a</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">./views/b</span><span style="color:#710">'</span></span> ]
<span style="color:#080;font-weight:bold">end</span>

<span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">find_template</span>(views, name, engine, &amp;block)
  Array(views).each <span style="color:#080;font-weight:bold">do</span> |v|
    <span style="color:#080;font-weight:bold">super</span>(v, name, engine, &amp;block)
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Filters'></a>
<h2>Filters</h2>

<p>Before filters are evaluated before each request within the same
context as the routes will be and can modify the request and response. Instance
variables set in filters are accessible by routes and templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>before <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#33B">@note</span> = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hi!</span><span style="color:#710">'</span></span>
  request.path_info = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo/bar/baz</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo/*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#33B">@note</span> <span style="color:#777">#=&gt; 'Hi!'</span>
  params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">splat</span><span style="color:#710">'</span></span>] <span style="color:#777">#=&gt; 'bar/baz'</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>After filters are evaluated after each request within the same context as the
routes will be and can also modify the request and response. Instance variables
set in before filters and routes are accessible by after filters:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>after <span style="color:#080;font-weight:bold">do</span>
  puts response.status
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Note: Unless you use the <code>body</code> method rather than just returning a String from
the routes, the body will not yet be available in the after filter, since it is
generated later on.</p>

<p>Filters optionally take a pattern, causing them to be evaluated only if the
request path matches that pattern:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>before <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/protected/*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  authenticate!
<span style="color:#080;font-weight:bold">end</span>

after <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/create/:slug</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span> |slug|
  session[<span style="color:#A60">:last_slug</span>] = slug
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Like routes, filters also take conditions:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>before <span style="color:#A60">:agent</span> =&gt; <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Songbird</span><span style="color:#404">/</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># ...</span>
<span style="color:#080;font-weight:bold">end</span>

after <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/blog/*</span><span style="color:#710">'</span></span>, <span style="color:#A60">:host_name</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">example.com</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># ...</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Helpers'></a>
<h2>Helpers</h2>

<p>Use the top-level <code>helpers</code> method to define helper methods for use in
route handlers and templates:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>helpers <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">bar</span>(name)
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>name<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">bar</span><span style="color:#710">"</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  bar(params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">name</span><span style="color:#710">'</span></span>])
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Alternatively, helper methods can be separately defined in a module:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">FooUtils</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">foo</span>(name) <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>name<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">foo</span><span style="color:#710">"</span></span> <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

<span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">BarUtils</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">bar</span>(name) <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>name<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">bar</span><span style="color:#710">"</span></span> <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

helpers <span style="color:#036;font-weight:bold">FooUtils</span>, <span style="color:#036;font-weight:bold">BarUtils</span>
</pre></div>
</div>
</div>

<p>The effect is the same as including the modules in the application class.</p>

<a name='Using%20Sessions'></a>
<h3>Using Sessions</h3>

<p>A session is used to keep state during requests. If activated, you have one
session hash per user session:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>enable <span style="color:#A60">:sessions</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">value = </span><span style="color:#710">"</span></span> &lt;&lt; session[<span style="color:#A60">:value</span>].inspect
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:value</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  session[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">value</span><span style="color:#710">'</span></span>] = params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">value</span><span style="color:#710">'</span></span>]
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Note that <code>enable :sessions</code> actually stores all data in a cookie. This
might not always be what you want (storing lots of data will increase your
traffic, for instance). You can use any Rack session middleware: in order to
do so, do <strong>not</strong> call <code>enable :sessions</code>, but instead pull in your
middleware of choice as you would any other middleware:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Session</span>::<span style="color:#036;font-weight:bold">Pool</span>, <span style="color:#A60">:expire_after</span> =&gt; <span style="color:#00D">2592000</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">value = </span><span style="color:#710">"</span></span> &lt;&lt; session[<span style="color:#A60">:value</span>].inspect
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:value</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  session[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">value</span><span style="color:#710">'</span></span>] = params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">value</span><span style="color:#710">'</span></span>]
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>To improve security, the session data in the cookie is signed with a session
secret. A random secret is generated for you by Sinatra. However, since this
secret will change with every start of your application, you might want to
set the secret yourself, so all your application instances share it:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:session_secret</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">super secret</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>If you want to configure it further, you may also store a hash with options in
the <code>sessions</code> setting:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:sessions</span>, <span style="color:#A60">:domain</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">foo.com</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>To share your session across other apps on subdomains of foo.com, prefix the
domain with a <em>.</em> like this instead:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:sessions</span>, <span style="color:#A60">:domain</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">.foo.com</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<a name='Halting'></a>
<h3>Halting</h3>

<p>To immediately stop a request within a filter or route use:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>halt
</pre></div>
</div>
</div>

<p>You can also specify the status when halting:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>halt <span style="color:#00D">410</span>
</pre></div>
</div>
</div>

<p>Or the body:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>halt <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">this will be the body</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>Or both:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>halt <span style="color:#00D">401</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">go away!</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>With headers:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>halt <span style="color:#00D">402</span>, {<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Content-Type</span><span style="color:#710">'</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">text/plain</span><span style="color:#710">'</span></span>}, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">revenge</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>It is of course possible to combine a template with <code>halt</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>halt erb(<span style="color:#A60">:error</span>)
</pre></div>
</div>
</div>

<a name='Passing'></a>
<h3>Passing</h3>

<p>A route can punt processing to the next matching route using <code>pass</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/guess/:who</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  pass <span style="color:#080;font-weight:bold">unless</span> params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">who</span><span style="color:#710">'</span></span>] == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Frank</span><span style="color:#710">'</span></span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">You got me!</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/guess/*</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">You missed!</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>The route block is immediately exited and control continues with the next
matching route. If no matching route is found, a 404 is returned.</p>

<a name='Triggering%20Another%20Route'></a>
<h3>Triggering Another Route</h3>

<p>Sometimes <code>pass</code> is not what you want, instead you would like to get the result
of calling another route. Simply use <code>call</code> to achieve this:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  status, headers, body = call env.merge(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">PATH_INFO</span><span style="color:#710">"</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span>)
  [status, headers, body.map(&amp;<span style="color:#A60">:upcase</span>)]
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">bar</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Note that in the example above, you would ease testing and increase performance
by simply moving <code>"bar"</code> into a helper used by both <code>/foo</code> and <code>/bar</code>.</p>

<p>If you want the request to be sent to the same application instance rather than
a duplicate, use <code>call!</code> instead of <code>call</code>.</p>

<p>Check out the Rack specification if you want to learn more about <code>call</code>.</p>

<a name='Setting%20Body,%20Status%20Code%20and%20Headers'></a>
<h3>Setting Body, Status Code and Headers</h3>

<p>It is possible and recommended to set the status code and response body with the
return value of the route block. However, in some scenarios you might want to
set the body at an arbitrary point in the execution flow. You can do so with the
<code>body</code> helper method. If you do so, you can use that method from there on to
access the body:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  body <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">bar</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

after <span style="color:#080;font-weight:bold">do</span>
  puts body
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>It is also possible to pass a block to <code>body</code>, which will be executed by the
Rack handler (this can be used to implement streaming, see “Return Values”).</p>

<p>Similar to the body, you can also set the status code and headers:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  status <span style="color:#00D">418</span>
  headers \
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Allow</span><span style="color:#710">"</span></span>   =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">BREW, POST, GET, PROPFIND, WHEN</span><span style="color:#710">"</span></span>,
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Refresh</span><span style="color:#710">"</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt</span><span style="color:#710">"</span></span>
  body <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">I'm a tea pot!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Like <code>body</code>, <code>headers</code> and <code>status</code> with no arguments can be used to access
their current values.</p>

<a name='Streaming%20Responses'></a>
<h3>Streaming Responses</h3>

<p>Sometimes you want to start sending out data while still generating parts of
the response body. In extreme examples, you want to keep sending data until
the client closes the connection. You can use the <code>stream</code> helper to avoid
creating your own wrapper:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  stream <span style="color:#080;font-weight:bold">do</span> |out|
    out &lt;&lt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">It's gonna be legen -</span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span>
    sleep <span style="color:#60E">0.5</span>
    out &lt;&lt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> (wait for it) </span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span>
    sleep <span style="color:#00D">1</span>
    out &lt;&lt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">- dary!</span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This allows you to implement streaming APIs,
<a href="https://w3c.github.io/eventsource/">Server Sent Events</a>, and can be used as
the basis for <a href="https://en.wikipedia.org/wiki/WebSocket">WebSockets</a>. It can also be
used to increase throughput if some but not all content depends on a slow
resource.</p>

<p>Note that the streaming behavior, especially the number of concurrent requests,
highly depends on the web server used to serve the application. Some servers
might not even support streaming at all. If the server does not support
streaming, the body will be sent all at once after the block passed to <code>stream</code>
finishes executing. Streaming does not work at all with Shotgun.</p>

<p>If the optional parameter is set to <code>keep_open</code>, it will not call <code>close</code> on
the stream object, allowing you to close it at any later point in the
execution flow. This only works on evented servers, like Thin and Rainbows.
Other servers will still close the stream:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># long polling</span>

set <span style="color:#A60">:server</span>, <span style="color:#A60">:thin</span>
connections = []

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/subscribe</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># register a client's interest in server events</span>
  stream(<span style="color:#A60">:keep_open</span>) <span style="color:#080;font-weight:bold">do</span> |out|
    connections &lt;&lt; out
    <span style="color:#777"># purge dead connections</span>
    connections.reject!(&amp;<span style="color:#A60">:closed?</span>)
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

post <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/:message</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  connections.each <span style="color:#080;font-weight:bold">do</span> |out|
    <span style="color:#777"># notify client that a new message has arrived</span>
    out &lt;&lt; params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">message</span><span style="color:#710">'</span></span>] &lt;&lt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#b0b">\n</span><span style="color:#710">"</span></span>

    <span style="color:#777"># indicate client to connect again</span>
    out.close
  <span style="color:#080;font-weight:bold">end</span>

  <span style="color:#777"># acknowledge</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">message received</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Logging'></a>
<h3>Logging</h3>

<p>In the request scope, the <code>logger</code> helper exposes a <code>Logger</code> instance:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  logger.info <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">loading data</span><span style="color:#710">"</span></span>
  <span style="color:#777"># ...</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This logger will automatically take your Rack handler’s logging settings into
account. If logging is disabled, this method will return a dummy object, so
you do not have to worry about it in your routes and filters.</p>

<p>Note that logging is only enabled for <code>Sinatra::Application</code> by default, so if
you inherit from <code>Sinatra::Base</code>, you probably want to enable it yourself:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  configure <span style="color:#A60">:production</span>, <span style="color:#A60">:development</span> <span style="color:#080;font-weight:bold">do</span>
    enable <span style="color:#A60">:logging</span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>To avoid any logging middleware to be set up, set the <code>logging</code> setting to
<code>nil</code>. However, keep in mind that <code>logger</code> will in that case return <code>nil</code>. A
common use case is when you want to set your own logger. Sinatra will use
whatever it will find in <code>env['rack.logger']</code>.</p>

<a name='Mime%20Types'></a>
<h3>Mime Types</h3>

<p>When using <code>send_file</code> or static files you may have mime types Sinatra
doesn’t understand. Use <code>mime_type</code> to register them by file extension:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>configure <span style="color:#080;font-weight:bold">do</span>
  mime_type <span style="color:#A60">:foo</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">text/foo</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You can also use it with the <code>content_type</code> helper:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  content_type <span style="color:#A60">:foo</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">foo foo foo</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Generating%20URLs'></a>
<h3>Generating URLs</h3>

<p>For generating URLs you should use the <code>url</code> helper method, for instance, in
Haml:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>%a{<span style="color:#A60">:href</span> =&gt; url(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span>)} foo
</pre></div>
</div>
</div>

<p>It takes reverse proxies and Rack routers into account, if present.</p>

<p>This method is also aliased to <code>to</code> (see below for an example).</p>

<a name='Browser%20Redirect'></a>
<h3>Browser Redirect</h3>

<p>You can trigger a browser redirect with the <code>redirect</code> helper method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  redirect to(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span>)
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Any additional parameters are handled like arguments passed to <code>halt</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>redirect to(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span>), <span style="color:#00D">303</span>
redirect <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">http://www.google.com/</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">wrong place, buddy</span><span style="color:#710">'</span></span>
</pre></div>
</div>
</div>

<p>You can also easily redirect back to the page the user came from with
<code>redirect back</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">&lt;a href='/bar'&gt;do something&lt;/a&gt;</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  do_something
  redirect back
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>To pass arguments with a redirect, either add them to the query:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>redirect to(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar?sum=42</span><span style="color:#710">'</span></span>)
</pre></div>
</div>
</div>

<p>Or use a session:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>enable <span style="color:#A60">:sessions</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  session[<span style="color:#A60">:secret</span>] = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">foo</span><span style="color:#710">'</span></span>
  redirect to(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span>)
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/bar</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  session[<span style="color:#A60">:secret</span>]
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Cache%20Control'></a>
<h3>Cache Control</h3>

<p>Setting your headers correctly is the foundation for proper HTTP caching.</p>

<p>You can easily set the Cache-Control header like this:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  cache_control <span style="color:#A60">:public</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">cache it!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Pro tip: Set up caching in a before filter:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>before <span style="color:#080;font-weight:bold">do</span>
  cache_control <span style="color:#A60">:public</span>, <span style="color:#A60">:must_revalidate</span>, <span style="color:#A60">:max_age</span> =&gt; <span style="color:#00D">60</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>If you are using the <code>expires</code> helper to set the corresponding header,
<code>Cache-Control</code> will be set automatically for you:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>before <span style="color:#080;font-weight:bold">do</span>
  expires <span style="color:#00D">500</span>, <span style="color:#A60">:public</span>, <span style="color:#A60">:must_revalidate</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>To properly use caches, you should consider using <code>etag</code> or <code>last_modified</code>.
It is recommended to call those helpers <em>before</em> doing any heavy lifting, as
they will immediately flush a response if the client already has the current
version in its cache:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/article/:id</span><span style="color:#710">"</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#33B">@article</span> = <span style="color:#036;font-weight:bold">Article</span>.find params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">id</span><span style="color:#710">'</span></span>]
  last_modified <span style="color:#33B">@article</span>.updated_at
  etag <span style="color:#33B">@article</span>.sha1
  erb <span style="color:#A60">:article</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>It is also possible to use a
<a href="https://en.wikipedia.org/wiki/HTTP_ETag#Strong_and_weak_validation">weak ETag</a>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>etag <span style="color:#33B">@article</span>.sha1, <span style="color:#A60">:weak</span>
</pre></div>
</div>
</div>

<p>These helpers will not do any caching for you, but rather feed the necessary
information to your cache. If you are looking for a quick reverse-proxy caching
solution, try <a href="https://github.com/rtomayko/rack-cache">rack-cache</a>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">rack/cache</span><span style="color:#710">"</span></span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">sinatra</span><span style="color:#710">"</span></span>

use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Cache</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  cache_control <span style="color:#A60">:public</span>, <span style="color:#A60">:max_age</span> =&gt; <span style="color:#00D">36000</span>
  sleep <span style="color:#00D">5</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">hello</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Use the <code>:static_cache_control</code> setting (see below) to add
<code>Cache-Control</code> header info to static files.</p>

<p>According to RFC 2616, your application should behave differently if the If-Match
or If-None-Match header is set to <code>*</code>, depending on whether the resource
requested is already in existence. Sinatra assumes resources for safe (like get)
and idempotent (like put) requests are already in existence, whereas other
resources (for instance post requests) are treated as new resources. You
can change this behavior by passing in a <code>:new_resource</code> option:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/create</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  etag <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>, <span style="color:#A60">:new_resource</span> =&gt; <span style="color:#069">true</span>
  <span style="color:#036;font-weight:bold">Article</span>.create
  erb <span style="color:#A60">:new_article</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>If you still want to use a weak ETag, pass in a <code>:kind</code> option:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>etag <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>, <span style="color:#A60">:new_resource</span> =&gt; <span style="color:#069">true</span>, <span style="color:#A60">:kind</span> =&gt; <span style="color:#A60">:weak</span>
</pre></div>
</div>
</div>

<a name='Sending%20Files'></a>
<h3>Sending Files</h3>

<p>To return the contents of a file as the response, you can use the <code>send_file</code>
helper method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  send_file <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">foo.png</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>It also takes options:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>send_file <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">foo.png</span><span style="color:#710">'</span></span>, <span style="color:#A60">:type</span> =&gt; <span style="color:#A60">:jpg</span>
</pre></div>
</div>
</div>

<p>The options are:</p>

<dl>
  <dt>filename</dt>
    <dd>File name to be used in the response, defaults to the real file name.</dd>

  <dt>last_modified</dt>
    <dd>Value for Last-Modified header, defaults to the file's mtime.</dd>

  <dt>type</dt>
    <dd>Value for Content-Type header, guessed from the file extension if
    missing.</dd>

  <dt>disposition</dt>
    <dd>
      Value for Content-Disposition header, possible values: <tt>nil</tt>
      (default), <tt>:attachment</tt> and <tt>:inline</tt>
    </dd>

  <dt>length</dt>
    <dd>Value for Content-Length header, defaults to file size.</dd>

  <dt>status</dt>
    <dd>
      Status code to be sent. Useful when sending a static file as an error page.

      If supported by the Rack handler, other means than streaming from the Ruby
      process will be used. If you use this helper method, Sinatra will
      automatically handle range requests.
    </dd>
</dl>

<a name='Accessing%20the%20Request%20Object'></a>
<h3>Accessing the Request Object</h3>

<p>The incoming request object can be accessed from request level (filter, routes,
error handlers) through the <code>request</code> method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># app running on http://example.com/example</span>
get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  t = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w[</span><span style="color:#D20">text/css text/html application/javascript</span><span style="color:#710">]</span></span>
  request.accept              <span style="color:#777"># ['text/html', '*/*']</span>
  request.accept? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">text/xml</span><span style="color:#710">'</span></span>  <span style="color:#777"># true</span>
  request.preferred_type(t)   <span style="color:#777"># 'text/html'</span>
  request.body                <span style="color:#777"># request body sent by the client (see below)</span>
  request.scheme              <span style="color:#777"># "http"</span>
  request.script_name         <span style="color:#777"># "/example"</span>
  request.path_info           <span style="color:#777"># "/foo"</span>
  request.port                <span style="color:#777"># 80</span>
  request.request_method      <span style="color:#777"># "GET"</span>
  request.query_string        <span style="color:#777"># ""</span>
  request.content_length      <span style="color:#777"># length of request.body</span>
  request.media_type          <span style="color:#777"># media type of request.body</span>
  request.host                <span style="color:#777"># "example.com"</span>
  request.get?                <span style="color:#777"># true (similar methods for other verbs)</span>
  request.form_data?          <span style="color:#777"># false</span>
  request[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">some_param</span><span style="color:#710">"</span></span>]       <span style="color:#777"># value of some_param parameter. [] is a shortcut to the params hash.</span>
  request.referrer            <span style="color:#777"># the referrer of the client or '/'</span>
  request.user_agent          <span style="color:#777"># user agent (used by :agent condition)</span>
  request.cookies             <span style="color:#777"># hash of browser cookies</span>
  request.xhr?                <span style="color:#777"># is this an ajax request?</span>
  request.url                 <span style="color:#777"># "http://example.com/example/foo"</span>
  request.path                <span style="color:#777"># "/example/foo"</span>
  request.ip                  <span style="color:#777"># client IP address</span>
  request.secure?             <span style="color:#777"># false (would be true over ssl)</span>
  request.forwarded?          <span style="color:#777"># true (if running behind a reverse proxy)</span>
  request.env                 <span style="color:#777"># raw env hash handed in by Rack</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Some options, like <code>script_name</code> or <code>path_info</code>, can also be written:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>before { request.path_info = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/</span><span style="color:#710">"</span></span> }

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/</span><span style="color:#710">"</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">all requests end up here</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>The <code>request.body</code> is an IO or StringIO object:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>post <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/api</span><span style="color:#710">"</span></span> <span style="color:#080;font-weight:bold">do</span>
  request.body.rewind  <span style="color:#777"># in case someone already read it</span>
  data = <span style="color:#036;font-weight:bold">JSON</span>.parse request.body.read
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>data[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">name</span><span style="color:#710">'</span></span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Attachments'></a>
<h3>Attachments</h3>

<p>You can use the <code>attachment</code> helper to tell the browser the response should be
stored on disk rather than displayed in the browser:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  attachment
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">store it!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You can also pass it a file name:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  attachment <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">info.txt</span><span style="color:#710">"</span></span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">store it!</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Dealing%20with%20Date%20and%20Time'></a>
<h3>Dealing with Date and Time</h3>

<p>Sinatra offers a <code>time_for</code> helper method that generates a Time object from the
given value. It is also able to convert <code>DateTime</code>, <code>Date</code> and similar classes:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  pass <span style="color:#080;font-weight:bold">if</span> <span style="color:#036;font-weight:bold">Time</span>.now &gt; time_for(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Dec 23, 2012</span><span style="color:#710">'</span></span>)
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">still time</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This method is used internally by <code>expires</code>, <code>last_modified</code> and akin. You can
therefore easily extend the behavior of those methods by overriding <code>time_for</code>
in your application:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>helpers <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">time_for</span>(value)
    <span style="color:#080;font-weight:bold">case</span> value
    <span style="color:#080;font-weight:bold">when</span> <span style="color:#A60">:yesterday</span> <span style="color:#080;font-weight:bold">then</span> <span style="color:#036;font-weight:bold">Time</span>.now - <span style="color:#00D">24</span>*<span style="color:#00D">60</span>*<span style="color:#00D">60</span>
    <span style="color:#080;font-weight:bold">when</span> <span style="color:#A60">:tomorrow</span>  <span style="color:#080;font-weight:bold">then</span> <span style="color:#036;font-weight:bold">Time</span>.now + <span style="color:#00D">24</span>*<span style="color:#00D">60</span>*<span style="color:#00D">60</span>
    <span style="color:#080;font-weight:bold">else</span> <span style="color:#080;font-weight:bold">super</span>
    <span style="color:#080;font-weight:bold">end</span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  last_modified <span style="color:#A60">:yesterday</span>
  expires <span style="color:#A60">:tomorrow</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">hello</span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Looking%20Up%20Template%20Files'></a>
<h3>Looking Up Template Files</h3>

<p>The <code>find_template</code> helper is used to find template files for rendering:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>find_template settings.views, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">foo</span><span style="color:#710">'</span></span>, <span style="color:#036;font-weight:bold">Tilt</span>[<span style="color:#A60">:haml</span>] <span style="color:#080;font-weight:bold">do</span> |file|
  puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">could be </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>file<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This is not really useful. But it is useful that you can actually override this
method to hook in your own lookup mechanism. For instance, if you want to be
able to use more than one view directory:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:views</span>, [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">views</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">templates</span><span style="color:#710">'</span></span>]

helpers <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">find_template</span>(views, name, engine, &amp;block)
    Array(views).each { |v| <span style="color:#080;font-weight:bold">super</span>(v, name, engine, &amp;block) }
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Another example would be using different directories for different engines:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:views</span>, <span style="color:#A60">:sass</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">views/sass</span><span style="color:#710">'</span></span>, <span style="color:#A60">:haml</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">templates</span><span style="color:#710">'</span></span>, <span style="color:#A60">:default</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">views</span><span style="color:#710">'</span></span>

helpers <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">find_template</span>(views, name, engine, &amp;block)
    _, folder = views.detect { |k,v| engine == <span style="color:#036;font-weight:bold">Tilt</span>[k] }
    folder ||= views[<span style="color:#A60">:default</span>]
    <span style="color:#080;font-weight:bold">super</span>(folder, name, engine, &amp;block)
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You can also easily wrap this up in an extension and share with others!</p>

<p>Note that <code>find_template</code> does not check if the file really exists but
rather calls the given block for all possible paths. This is not a performance
issue, since <code>render</code> will use <code>break</code> as soon as a file is found. Also,
template locations (and content) will be cached if you are not running in
development mode. You should keep that in mind if you write a really crazy
method.</p>

<a name='Configuration'></a>
<h2>Configuration</h2>

<p>Run once, at startup, in any environment:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>configure <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#777"># setting one option</span>
  set <span style="color:#A60">:option</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">value</span><span style="color:#710">'</span></span>

  <span style="color:#777"># setting multiple options</span>
  set <span style="color:#A60">:a</span> =&gt; <span style="color:#00D">1</span>, <span style="color:#A60">:b</span> =&gt; <span style="color:#00D">2</span>

  <span style="color:#777"># same as `set :option, true`</span>
  enable <span style="color:#A60">:option</span>

  <span style="color:#777"># same as `set :option, false`</span>
  disable <span style="color:#A60">:option</span>

  <span style="color:#777"># you can also have dynamic settings with blocks</span>
  set(<span style="color:#A60">:css_dir</span>) { <span style="color:#036;font-weight:bold">File</span>.join(views, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">css</span><span style="color:#710">'</span></span>) }
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Run only when the environment (<code>RACK_ENV</code> environment variable) is set to
<code>:production</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>configure <span style="color:#A60">:production</span> <span style="color:#080;font-weight:bold">do</span>
  ...
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Run when the environment is set to either <code>:production</code> or <code>:test</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>configure <span style="color:#A60">:production</span>, <span style="color:#A60">:test</span> <span style="color:#080;font-weight:bold">do</span>
  ...
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You can access those options via <code>settings</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>configure <span style="color:#080;font-weight:bold">do</span>
  set <span style="color:#A60">:foo</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">bar</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  settings.foo? <span style="color:#777"># =&gt; true</span>
  settings.foo  <span style="color:#777"># =&gt; 'bar'</span>
  ...
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Configuring%20attack%20protection'></a>
<h3>Configuring attack protection</h3>

<p>Sinatra is using
<a href="https://github.com/sinatra/rack-protection#readme">Rack::Protection</a> to defend
your application against common, opportunistic attacks. You can easily disable
this behavior (which will open up your application to tons of common
vulnerabilities):</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>disable <span style="color:#A60">:protection</span>
</pre></div>
</div>
</div>

<p>To skip a single defense layer, set <code>protection</code> to an options hash:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:protection</span>, <span style="color:#A60">:except</span> =&gt; <span style="color:#A60">:path_traversal</span>
</pre></div>
</div>
</div>
<p>You can also hand in an array in order to disable a list of protections:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:protection</span>, <span style="color:#A60">:except</span> =&gt; [<span style="color:#A60">:path_traversal</span>, <span style="color:#A60">:session_hijacking</span>]
</pre></div>
</div>
</div>

<p>By default, Sinatra will only set up session based protection if <code>:sessions</code>
has been enabled. Sometimes you want to set up sessions on your own, though. In
that case you can get it to set up session based protections by passing the
<code>:session</code> option:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Session</span>::<span style="color:#036;font-weight:bold">Pool</span>
set <span style="color:#A60">:protection</span>, <span style="color:#A60">:session</span> =&gt; <span style="color:#069">true</span>
</pre></div>
</div>
</div>

<a name='Available%20Settings'></a>
<h3>Available Settings</h3>

<dl>
  <dt>absolute_redirects</dt>
  <dd>
    If disabled, Sinatra will allow relative redirects, however, Sinatra will no
    longer conform with RFC 2616 (HTTP 1.1), which only allows absolute redirects.
  </dd>
  <dd>
    Enable if your app is running behind a reverse proxy that has not been set up
    properly. Note that the <tt>url</tt> helper will still produce absolute URLs, unless you
    pass in <tt>false</tt> as the second parameter.
  </dd>
  <dd>Disabled by default.</dd>

  <dt>add_charset</dt>
  <dd>
    Mime types the <tt>content_type</tt> helper will automatically add the charset info to.
    You should add to it rather than overriding this option:
    <tt>settings.add_charset &lt;&lt; "application/foobar"</tt>
  </dd>

  <dt>app_file</dt>
  <dd>
    Path to the main application file, used to detect project root, views and public
    folder and inline templates.
  </dd>

  <dt>bind</dt>
  <dd>IP address to bind to (default: <tt>0.0.0.0</tt> <em>or</em>
  <tt>localhost</tt> if your `environment` is set to development). Only used
  for built-in server.</dd>

  <dt>default_encoding</dt>
  <dd>Encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>

  <dt>dump_errors</dt>
  <dd>Display errors in the log.</dd>

  <dt>environment</dt>
  <dd>
    Current environment. Defaults to <tt>ENV['RACK_ENV']</tt>, or
    <tt>"development"</tt> if not available.
  </dd>

  <dt>logging</dt>
  <dd>Use the logger.</dd>

  <dt>lock</dt>
  <dd>
    Places a lock around every request, only running processing on request
    per Ruby process concurrently.
  </dd>
  <dd>Enabled if your app is not thread-safe. Disabled by default.</dd>

  <dt>method_override</dt>
  <dd>
    Use <tt>_method</tt> magic to allow put/delete forms in browsers that
    don't support it.
  </dd>

  <dt>port</dt>
  <dd>Port to listen on. Only used for built-in server.</dd>

  <dt>prefixed_redirects</dt>
  <dd>
    Whether or not to insert <tt>request.script_name</tt> into redirects if no
    absolute path is given. That way <tt>redirect '/foo'</tt> would behave like
    <tt>redirect to('/foo')</tt>. Disabled by default.
  </dd>

  <dt>protection</dt>
  <dd>Whether or not to enable web attack protections. See protection section
  above.</dd>

  <dt>public_dir</dt>
  <dd>Alias for <tt>public_folder</tt>. See below.</dd>

  <dt>public_folder</dt>
  <dd>
    Path to the folder public files are served from. Only used if static
    file serving is enabled (see <tt>static</tt> setting below). Inferred from
    <tt>app_file</tt> setting if not set.
  </dd>

  <dt>reload_templates</dt>
  <dd>
    Whether or not to reload templates between requests. Enabled in development
    mode.
  </dd>

  <dt>root</dt>
  <dd>
    Path to project root folder. Inferred from <tt>app_file</tt> setting if not
    set.
  </dd>

  <dt>raise_errors</dt>
  <dd>
    Raise exceptions (will stop application). Enabled by default when
    <tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
  </dd>

  <dt>run</dt>
  <dd>
    If enabled, Sinatra will handle starting the web server. Do not
    enable if using rackup or other means.
  </dd>

  <dt>running</dt>
  <dd>Is the built-in server running now? Do not change this setting!</dd>

  <dt>server</dt>
  <dd>
    Server or list of servers to use for built-in server. Order indicates
    priority, default depends on Ruby implementation.
  </dd>

  <dt>sessions</dt>
  <dd>
    Enable cookie-based sessions support using <tt>Rack::Session::Cookie</tt>.
    See 'Using Sessions' section for more information.
  </dd>

  <dt>show_exceptions</dt>
  <dd>
    Show a stack trace in the browser when an exception happens. Enabled by
    default when <tt>environment</tt> is set to <tt>"development"</tt>,
    disabled otherwise.
  </dd>
  <dd>
    Can also be set to <tt>:after_handler</tt> to trigger app-specified error
    handling before showing a stack trace in the browser.
  </dd>

  <dt>static</dt>
  <dd>Whether Sinatra should handle serving static files.</dd>
  <dd>Disable when using a server able to do this on its own.</dd>
  <dd>Disabling will boost performance.</dd>
  <dd>
    Enabled by default in classic style, disabled for modular apps.
  </dd>

  <dt>static_cache_control</dt>
  <dd>
    When Sinatra is serving static files, set this to add <tt>Cache-Control</tt>
    headers to the responses. Uses the <tt>cache_control</tt> helper. Disabled
    by default.
  </dd>
  <dd>
    Use an explicit array when setting multiple values:
    <tt>set :static_cache_control, [:public, :max_age =&gt; 300]</tt>
  </dd>

  <dt>threaded</dt>
  <dd>
    If set to <tt>true</tt>, will tell Thin to use <tt>EventMachine.defer</tt>
    for processing the request.
  </dd>

  <dt>traps</dt>
  <dd>Whether Sinatra should handle system signals.</dd>

  <dt>views</dt>
  <dd>
    Path to the views folder. Inferred from <tt>app_file</tt> setting if
    not set.
  </dd>

  <dt>x_cascade</dt>
  <dd>
    Whether or not to set the X-Cascade header if no route matches.
    Defaults to <tt>true</tt>.
  </dd>
</dl>

<a name='Environments'></a>
<h2>Environments</h2>

<p>There are three predefined <code>environments</code>: <code>"development"</code>, <code>"production"</code> and
<code>"test"</code>. Environments can be set through the <code>RACK_ENV</code> environment variable.
The default value is <code>"development"</code>. In the <code>"development"</code> environment all
templates are reloaded between requests, and special <code>not_found</code> and <code>error</code>
handlers display stack traces in your browser. In the <code>"production"</code> and
<code>"test"</code> environments, templates are cached by default.</p>

<p>To run different environments, set the <code>RACK_ENV</code> environment variable:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>RACK_ENV=production ruby my_app.rb
</pre></div>
</div>
</div>

<p>You can use predefined methods: <code>development?</code>, <code>test?</code> and <code>production?</code> to
check the current environment setting:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#080;font-weight:bold">if</span> settings.development?
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">development!</span><span style="color:#710">"</span></span>
  <span style="color:#080;font-weight:bold">else</span>
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">not development!</span><span style="color:#710">"</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Error%20Handling'></a>
<h2>Error Handling</h2>

<p>Error handlers run within the same context as routes and before filters, which
means you get all the goodies it has to offer, like <code>haml</code>,
<code>erb</code>, <code>halt</code>, etc.</p>

<a name='Not%20Found'></a>
<h3>Not Found</h3>

<p>When a <code>Sinatra::NotFound</code> exception is raised, or the response’s status
code is 404, the <code>not_found</code> handler is invoked:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>not_found <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">This is nowhere to be found.</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Error'></a>
<h3>Error</h3>

<p>The <code>error</code> handler is invoked any time an exception is raised from a route
block or a filter. But note in development it will only run if you set the
show exceptions option to <code>:after_handler</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>set <span style="color:#A60">:show_exceptions</span>, <span style="color:#A60">:after_handler</span>
</pre></div>
</div>
</div>

<p>The exception object can be obtained from the <code>sinatra.error</code> Rack variable:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>error <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Sorry there was a nasty error - </span><span style="color:#710">'</span></span> + env[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra.error</span><span style="color:#710">'</span></span>].message
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Custom errors:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>error <span style="color:#036;font-weight:bold">MyCustomError</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">So what happened was...</span><span style="color:#710">'</span></span> + env[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra.error</span><span style="color:#710">'</span></span>].message
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Then, if this happens:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  raise <span style="color:#036;font-weight:bold">MyCustomError</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">something bad</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You get this:</p>

<pre><code>So what happened was... something bad
</code></pre>

<p>Alternatively, you can install an error handler for a status code:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>error <span style="color:#00D">403</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Access forbidden</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/secret</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="color:#00D">403</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Or a range:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>error <span style="color:#00D">400</span>..<span style="color:#00D">510</span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Boom</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Sinatra installs special <code>not_found</code> and <code>error</code> handlers when
running under the development environment to display nice stack traces
and additional debugging information in your browser.</p>

<a name='Rack%20Middleware'></a>
<h2>Rack Middleware</h2>

<p>Sinatra rides on <a href="http://rack.github.io/">Rack</a>, a minimal standard
interface for Ruby web frameworks. One of Rack’s most interesting capabilities
for application developers is support for “middleware” – components that sit
between the server and your application monitoring and/or manipulating the
HTTP request/response to provide various types of common functionality.</p>

<p>Sinatra makes building Rack middleware pipelines a cinch via a top-level
<code>use</code> method:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">my_custom_middleware</span><span style="color:#710">'</span></span>

use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Lint</span>
use <span style="color:#036;font-weight:bold">MyCustomMiddleware</span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/hello</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello World</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>The semantics of <code>use</code> are identical to those defined for the
<a href="http://www.rubydoc.info/github/rack/rack/master/Rack/Builder">Rack::Builder</a> DSL
(most frequently used from rackup files). For example, the <code>use</code> method
accepts multiple/variable args as well as blocks:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>use <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Auth</span>::<span style="color:#036;font-weight:bold">Basic</span> <span style="color:#080;font-weight:bold">do</span> |username, password|
  username == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">admin</span><span style="color:#710">'</span></span> &amp;&amp; password == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">secret</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Rack is distributed with a variety of standard middleware for logging,
debugging, URL routing, authentication, and session handling. Sinatra uses
many of these components automatically based on configuration so you
typically don’t have to <code>use</code> them explicitly.</p>

<p>You can find useful middleware in
<a href="https://github.com/rack/rack/tree/master/lib/rack">rack</a>,
<a href="https://github.com/rack/rack-contrib#readm">rack-contrib</a>,
or in the <a href="https://github.com/rack/rack/wiki/List-of-Middleware">Rack wiki</a>.</p>

<a name='Testing'></a>
<h2>Testing</h2>

<p>Sinatra tests can be written using any Rack-based testing library or framework.
<a href="http://www.rubydoc.info/github/brynary/rack-test/master/frames">Rack::Test</a>
is recommended:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">my_sinatra_app</span><span style="color:#710">'</span></span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">minitest/autorun</span><span style="color:#710">'</span></span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rack/test</span><span style="color:#710">'</span></span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyAppTest</span> &lt; <span style="color:#036;font-weight:bold">Minitest</span>::<span style="color:#036;font-weight:bold">Test</span>
  include <span style="color:#036;font-weight:bold">Rack</span>::<span style="color:#036;font-weight:bold">Test</span>::<span style="color:#036;font-weight:bold">Methods</span>

  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">app</span>
    <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Application</span>
  <span style="color:#080;font-weight:bold">end</span>

  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">test_my_default</span>
    get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>
    assert_equal <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello World!</span><span style="color:#710">'</span></span>, last_response.body
  <span style="color:#080;font-weight:bold">end</span>

  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">test_with_params</span>
    get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/meet</span><span style="color:#710">'</span></span>, <span style="color:#A60">:name</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Frank</span><span style="color:#710">'</span></span>
    assert_equal <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello Frank!</span><span style="color:#710">'</span></span>, last_response.body
  <span style="color:#080;font-weight:bold">end</span>

  <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">test_with_rack_env</span>
    get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>, {}, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">HTTP_USER_AGENT</span><span style="color:#710">'</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Songbird</span><span style="color:#710">'</span></span>
    assert_equal <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">You're using Songbird!</span><span style="color:#710">"</span></span>, last_response.body
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Note: If you are using Sinatra in the modular style, replace
<code>Sinatra::Application</code> above with the class name of your app.</p>

<a name='Sinatra::Base%20-%20Middleware,%20Libraries,%20and%20Modular%20Apps'></a>
<h2>Sinatra::Base - Middleware, Libraries, and Modular Apps</h2>

<p>Defining your app at the top-level works well for micro-apps but has
considerable drawbacks when building reusable components such as Rack
middleware, Rails metal, simple libraries with a server component, or even
Sinatra extensions. The top-level assumes a micro-app style configuration
(e.g., a single application file, <code>./public</code> and <code>./views</code>
directories, logging, exception detail page, etc.). That’s where
<code>Sinatra::Base</code> comes into play:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  set <span style="color:#A60">:sessions</span>, <span style="color:#069">true</span>
  set <span style="color:#A60">:foo</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">bar</span><span style="color:#710">'</span></span>

  get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello world!</span><span style="color:#710">'</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>The methods available to <code>Sinatra::Base</code> subclasses are exactly the same as
those available via the top-level DSL. Most top-level apps can be converted to
<code>Sinatra::Base</code> components with two modifications:</p>

<ul>
  <li>Your file should require <code>sinatra/base</code> instead of <code>sinatra</code>;
otherwise, all of Sinatra’s DSL methods are imported into the main
namespace.</li>
  <li>Put your app’s routes, error handlers, filters, and options in a subclass
of <code>Sinatra::Base</code>.</li>
</ul>

<p><code>Sinatra::Base</code> is a blank slate. Most options are disabled by default,
including the built-in server. See
<a href="http://www.sinatrarb.com/configuration.html">Configuring Settings</a>
for details on available options and their behavior. If you want
behavior more similar to when you define your app at the top level (also
known as Classic style), you
can subclass <code>Sinatra::Application</code>.</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Application</span>
  get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello world!</span><span style="color:#710">'</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Modular%20vs.%20Classic%20Style'></a>
<h3>Modular vs. Classic Style</h3>

<p>Contrary to common belief, there is nothing wrong with the classic style. If it
suits your application, you do not have to switch to a modular application.</p>

<p>The main disadvantage of using the classic style rather than the modular style
is that you will only have one Sinatra application per Ruby process. If you
plan to use more than one, switch to the modular style. There is no reason you
cannot mix the modular and the classic styles.</p>

<p>If switching from one style to the other, you should be aware of slightly
different default settings:</p>

<table>
  <tr>
    <th>Setting</th>
    <th>Classic</th>
    <th>Modular</th>
    <th>Modular</th>
  </tr>

  <tr>
    <td>app_file</td>
    <td>file loading sinatra</td>
    <td>file subclassing Sinatra::Base</td>
    <td>file subclassing Sinatra::Application</td>
  </tr>

  <tr>
    <td>run</td>
    <td>$0 == app_file</td>
    <td>false</td>
    <td>false</td>
  </tr>

  <tr>
    <td>logging</td>
    <td>true</td>
    <td>false</td>
    <td>true</td>
  </tr>

  <tr>
    <td>method_override</td>
    <td>true</td>
    <td>false</td>
    <td>true</td>
  </tr>

  <tr>
    <td>inline_templates</td>
    <td>true</td>
    <td>false</td>
    <td>true</td>
  </tr>

  <tr>
    <td>static</td>
    <td>true</td>
    <td>false</td>
    <td>true</td>
  </tr>
</table>

<a name='Serving%20a%20Modular%20Application'></a>
<h3>Serving a Modular Application</h3>

<p>There are two common options for starting a modular app, actively starting with
<code>run!</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># my_app.rb</span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  <span style="color:#777"># ... app code here ...</span>

  <span style="color:#777"># start the server if ruby file executed directly</span>
  run! <span style="color:#080;font-weight:bold">if</span> app_file == <span style="color:#d70">$0</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>Start with:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>ruby my_app.rb
</pre></div>
</div>
</div>

<p>Or with a <code>config.ru</code> file, which allows using any Rack handler:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># config.ru (run with rackup)</span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">./my_app</span><span style="color:#710">'</span></span>
run <span style="color:#036;font-weight:bold">MyApp</span>
</pre></div>
</div>
</div>

<p>Run:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>rackup -p 4567
</pre></div>
</div>
</div>

<a name='Using%20a%20Classic%20Style%20Application%20with%20a%20config.ru'></a>
<h3>Using a Classic Style Application with a config.ru</h3>

<p>Write your app file:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># app.rb</span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>

get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
  <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello world!</span><span style="color:#710">'</span></span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>And a corresponding <code>config.ru</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">./app</span><span style="color:#710">'</span></span>
run <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Application</span>
</pre></div>
</div>
</div>

<a name='When%20to%20use%20a%20config.ru?'></a>
<h3>When to use a config.ru?</h3>

<p>A <code>config.ru</code> file is recommended if:</p>

<ul>
  <li>You want to deploy with a different Rack handler (Passenger, Unicorn,
Heroku, …).</li>
  <li>You want to use more than one subclass of <code>Sinatra::Base</code>.</li>
  <li>You want to use Sinatra only for middleware, and not as an endpoint.</li>
</ul>

<p><strong>There is no need to switch to a <code>config.ru</code> simply because you switched to
the modular style, and you don’t have to use the modular style for running with
a <code>config.ru</code>.</strong></p>

<a name='Using%20Sinatra%20as%20Middleware'></a>
<h3>Using Sinatra as Middleware</h3>

<p>Not only is Sinatra able to use other Rack middleware, any Sinatra application
can in turn be added in front of any Rack endpoint as middleware itself. This
endpoint could be another Sinatra application, or any other Rack-based
application (Rails/Ramaze/Camping/…):</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">LoginScreen</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  enable <span style="color:#A60">:sessions</span>

  get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/login</span><span style="color:#710">'</span></span>) { haml <span style="color:#A60">:login</span> }

  post(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/login</span><span style="color:#710">'</span></span>) <span style="color:#080;font-weight:bold">do</span>
    <span style="color:#080;font-weight:bold">if</span> params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">name</span><span style="color:#710">'</span></span>] == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">admin</span><span style="color:#710">'</span></span> &amp;&amp; params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">password</span><span style="color:#710">'</span></span>] == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">admin</span><span style="color:#710">'</span></span>
      session[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">user_name</span><span style="color:#710">'</span></span>] = params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">name</span><span style="color:#710">'</span></span>]
    <span style="color:#080;font-weight:bold">else</span>
      redirect <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/login</span><span style="color:#710">'</span></span>
    <span style="color:#080;font-weight:bold">end</span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  <span style="color:#777"># middleware will run before filters</span>
  use <span style="color:#036;font-weight:bold">LoginScreen</span>

  before <span style="color:#080;font-weight:bold">do</span>
    <span style="color:#080;font-weight:bold">unless</span> session[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">user_name</span><span style="color:#710">'</span></span>]
      halt <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Access denied, please &lt;a href='/login'&gt;login&lt;/a&gt;.</span><span style="color:#710">"</span></span>
    <span style="color:#080;font-weight:bold">end</span>
  <span style="color:#080;font-weight:bold">end</span>

  get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>session[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">user_name</span><span style="color:#710">'</span></span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">.</span><span style="color:#710">"</span></span> }
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<a name='Dynamic%20Application%20Creation'></a>
<h3>Dynamic Application Creation</h3>

<p>Sometimes you want to create new applications at runtime without having to
assign them to a constant. You can do this with <code>Sinatra.new</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>
my_app = <span style="color:#036;font-weight:bold">Sinatra</span>.new { get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">hi</span><span style="color:#710">"</span></span> } }
my_app.run!
</pre></div>
</div>
</div>

<p>It takes the application to inherit from as an optional argument:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># config.ru (run with rackup)</span>
require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

controller = <span style="color:#036;font-weight:bold">Sinatra</span>.new <span style="color:#080;font-weight:bold">do</span>
  enable <span style="color:#A60">:logging</span>
  helpers <span style="color:#036;font-weight:bold">MyHelpers</span>
<span style="color:#080;font-weight:bold">end</span>

map(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/a</span><span style="color:#710">'</span></span>) <span style="color:#080;font-weight:bold">do</span>
  run <span style="color:#036;font-weight:bold">Sinatra</span>.new(controller) { get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span> } }
<span style="color:#080;font-weight:bold">end</span>

map(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/b</span><span style="color:#710">'</span></span>) <span style="color:#080;font-weight:bold">do</span>
  run <span style="color:#036;font-weight:bold">Sinatra</span>.new(controller) { get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span> } }
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>This is especially useful for testing Sinatra extensions or using Sinatra in
your own library.</p>

<p>This also makes using Sinatra as middleware extremely easy:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

use <span style="color:#036;font-weight:bold">Sinatra</span> <span style="color:#080;font-weight:bold">do</span>
  get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span>) { ... }
<span style="color:#080;font-weight:bold">end</span>

run <span style="color:#036;font-weight:bold">RailsProject</span>::<span style="color:#036;font-weight:bold">Application</span>
</pre></div>
</div>
</div>

<a name='Scopes%20and%20Binding'></a>
<h2>Scopes and Binding</h2>

<p>The scope you are currently in determines what methods and variables are
available.</p>

<a name='Application/Class%20Scope'></a>
<h3>Application/Class Scope</h3>

<p>Every Sinatra application corresponds to a subclass of <code>Sinatra::Base</code>.
If you are using the top-level DSL (<code>require 'sinatra'</code>), then this
class is <code>Sinatra::Application</code>, otherwise it is the subclass you
created explicitly. At class level you have methods like <code>get</code> or <code>before</code>, but
you cannot access the <code>request</code> or <code>session</code> objects, as there is only a
single application class for all requests.</p>

<p>Options created via <code>set</code> are methods at class level:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  <span style="color:#777"># Hey, I'm in the application scope!</span>
  set <span style="color:#A60">:foo</span>, <span style="color:#00D">42</span>
  foo <span style="color:#777"># =&gt; 42</span>

  get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/foo</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
    <span style="color:#777"># Hey, I'm no longer in the application scope!</span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You have the application scope binding inside:</p>

<ul>
  <li>Your application class body</li>
  <li>Methods defined by extensions</li>
  <li>The block passed to <code>helpers</code>
</li>
  <li>Procs/blocks used as value for <code>set</code>
</li>
  <li>The block passed to <code>Sinatra.new</code>
</li>
</ul>

<p>You can reach the scope object (the class) like this:</p>

<ul>
  <li>Via the object passed to configure blocks (<code>configure { |c| ... }</code>)</li>
  <li>
<code>settings</code> from within the request scope</li>
</ul>

<a name='Request/Instance%20Scope'></a>
<h3>Request/Instance Scope</h3>

<p>For every incoming request, a new instance of your application class is
created, and all handler blocks run in that scope. From within this scope you
can access the <code>request</code> and <code>session</code> objects or call rendering methods like
<code>erb</code> or <code>haml</code>. You can access the application scope from within the request
scope via the <code>settings</code> helper:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">MyApp</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  <span style="color:#777"># Hey, I'm in the application scope!</span>
  get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/define_route/:name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
    <span style="color:#777"># Request scope for '/define_route/:name'</span>
    <span style="color:#33B">@value</span> = <span style="color:#00D">42</span>

    settings.get(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">/</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>params[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">name</span><span style="color:#710">'</span></span>]<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>) <span style="color:#080;font-weight:bold">do</span>
      <span style="color:#777"># Request scope for "/#{params['name']}"</span>
      <span style="color:#33B">@value</span> <span style="color:#777"># =&gt; nil (not the same request)</span>
    <span style="color:#080;font-weight:bold">end</span>

    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Route defined!</span><span style="color:#710">"</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>
</pre></div>
</div>
</div>

<p>You have the request scope binding inside:</p>

<ul>
  <li>get, head, post, put, delete, options, patch, link and unlink blocks</li>
  <li>before and after filters</li>
  <li>helper methods</li>
  <li>templates/views</li>
</ul>

<a name='Delegation%20Scope'></a>
<h3>Delegation Scope</h3>

<p>The delegation scope just forwards methods to the class scope. However, it
does not behave exactly like the class scope, as you do not have the class
binding. Only methods explicitly marked for delegation are available, and you
do not share variables/state with the class scope (read: you have a different
<code>self</code>). You can explicitly add method delegations by calling
<code>Sinatra::Delegator.delegate :method_name</code>.</p>

<p>You have the delegate scope binding inside:</p>

<ul>
  <li>The top level binding, if you did <code>require "sinatra"</code>
</li>
  <li>An object extended with the <code>Sinatra::Delegator</code> mixin</li>
</ul>

<p>Have a look at the code for yourself: here’s the
<a href="https://github.com/sinatra/sinatra/blob/ca06364/lib/sinatra/base.rb#L1609-1633">Sinatra::Delegator mixin</a>
being <a href="https://github.com/sinatra/sinatra/blob/ca06364/lib/sinatra/main.rb#L28-30">extending the main object</a>.</p>

<a name='Command%20Line'></a>
<h2>Command Line</h2>

<p>Sinatra applications can be run directly:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
</pre></div>
</div>
</div>

<p>Options are:</p>

<pre><code>-h # help
-p # set the port (default is 4567)
-o # set the host (default is 0.0.0.0)
-e # set the environment (default is development)
-s # specify rack server/handler (default is thin)
-x # turn on the mutex lock (default is off)
</code></pre>

<a name='Multi-threading'></a>
<h3>Multi-threading</h3>

<p><em>Paraphrasing from <a href="http://stackoverflow.com/questions/6278817/is-sinatra-multi-threaded/6282999#6282999)">this StackOverflow answer</a> by Konstantin</em></p>

<p>Sinatra doesn’t impose any concurrency model, but leaves that to the
underlying Rack handler (server) like Thin, Puma or WEBrick. Sinatra
itself is thread-safe, so there won’t be any problem if the Rack handler
uses a threaded model of concurrency. This would mean that when starting
the server, you’d have to specify the correct invocation method for the
specific Rack handler. The following example is a demonstration of how
to start a multi-threaded Thin server:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre><span style="color:#777"># app.rb</span>

require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra/base</span><span style="color:#710">'</span></span>

<span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">App</span> &lt; <span style="color:#036;font-weight:bold">Sinatra</span>::<span style="color:#036;font-weight:bold">Base</span>
  get <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">/</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
    <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, World</span><span style="color:#710">"</span></span>
  <span style="color:#080;font-weight:bold">end</span>
<span style="color:#080;font-weight:bold">end</span>

<span style="color:#036;font-weight:bold">App</span>.run!

</pre></div>
</div>
</div>

<p>To start the server, the command would be:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>thin --threaded start
</pre></div>
</div>
</div>

<a name='Requirement'></a>
<h2>Requirement</h2>

<p>The following Ruby versions are officially supported:</p>
<dl>
  <dt>Ruby 1.8.7</dt>
  <dd>
    1.8.7 is fully supported, however, if nothing is keeping you from it, we
    recommend upgrading or switching to JRuby or Rubinius. Support for 1.8.7
    will not be dropped before Sinatra 2.0. Ruby 1.8.6 is no longer supported.
  </dd>

  <dt>Ruby 1.9.2</dt>
  <dd>
    1.9.2 is fully supported. Do not use 1.9.2p0, as it is known to cause
    segmentation faults when running Sinatra. Official support will continue
    at least until the release of Sinatra 1.5.
  </dd>

  <dt>Ruby 1.9.3</dt>
  <dd>
    1.9.3 is fully supported and recommended. Please note that switching to 1.9.3
    from an earlier version will invalidate all sessions. 1.9.3 will be supported
    until the release of Sinatra 2.0.
  </dd>

  <dt>Ruby 2.x</dt>
  <dd>
    2.x is fully supported and recommended. There are currently no plans to drop
    official support for it.
  </dd>

  <dt>Rubinius</dt>
  <dd>
    Rubinius is officially supported (Rubinius &gt;= 2.x). It is recommended to
    <tt>gem install puma</tt>.
  </dd>

  <dt>JRuby</dt>
  <dd>
    The latest stable release of JRuby is officially supported. It is not
    recommended to use C extensions with JRuby. It is recommended to
    <tt>gem install trinidad</tt>.
  </dd>
</dl>

<p>We also keep an eye on upcoming Ruby versions.</p>

<p>The following Ruby implementations are not officially supported but still are
known to run Sinatra:</p>

<ul>
  <li>Older versions of JRuby and Rubinius</li>
  <li>Ruby Enterprise Edition</li>
  <li>MacRuby, Maglev, IronRuby</li>
  <li>Ruby 1.9.0 and 1.9.1 (but we do recommend against using those)</li>
</ul>

<p>Not being officially supported means if things only break there and not on a
supported platform, we assume it’s not our issue but theirs.</p>

<p>We also run our CI against ruby-head (future releases of MRI), but we can’t
guarantee anything, since it is constantly moving. Expect upcoming 2.x releases
to be fully supported.</p>

<p>Sinatra should work on any operating system supported by the chosen Ruby
implementation.</p>

<p>If you run MacRuby, you should <code>gem install control_tower</code>.</p>

<p>Sinatra currently doesn’t run on Cardinal, SmallRuby, BlueRuby or any
Ruby version prior to 1.8.7.</p>

<a name='The%20Bleeding%20Edge'></a>
<h2>The Bleeding Edge</h2>

<p>If you would like to use Sinatra’s latest bleeding-edge code, feel free to run your
application against the master branch, it should be rather stable.</p>

<p>We also push out prerelease gems from time to time, so you can do a</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>gem install sinatra --pre
</pre></div>
</div>
</div>

<p>to get some of the latest features.</p>

<a name='With%20Bundler'></a>
<h3>With Bundler</h3>

<p>If you want to run your application with the latest Sinatra, using
<a href="http://bundler.io">Bundler</a> is the recommended way.</p>

<p>First, install bundler, if you haven’t:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>gem install bundler
</pre></div>
</div>
</div>

<p>Then, in your project directory, create a <code>Gemfile</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>source <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">https://rubygems.org</span><span style="color:#710">'</span></span>
gem <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sinatra</span><span style="color:#710">'</span></span>, <span style="color:#A60">:github</span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">sinatra/sinatra</span><span style="color:#710">"</span></span>

<span style="color:#777"># other dependencies</span>
gem <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">haml</span><span style="color:#710">'</span></span>                    <span style="color:#777"># for instance, if you use haml</span>
gem <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">activerecord</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">~&gt; 3.0</span><span style="color:#710">'</span></span>  <span style="color:#777"># maybe you also need ActiveRecord 3.x</span>
</pre></div>
</div>
</div>

<p>Note that you will have to list all your application’s dependencies in the <code>Gemfile</code>.
Sinatra’s direct dependencies (Rack and Tilt) will, however, be automatically
fetched and added by Bundler.</p>

<p>Now you can run your app like this:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>bundle exec ruby myapp.rb
</pre></div>
</div>
</div>

<a name='Roll%20Your%20Own'></a>
<h3>Roll Your Own</h3>

<p>Create a local clone and run your app with the <code>sinatra/lib</code> directory
on the <code>$LOAD_PATH</code>:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>cd myapp
git clone git://github.com/sinatra/sinatra.git
ruby -I sinatra/lib myapp.rb
</pre></div>
</div>
</div>

<p>To update the Sinatra sources in the future:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>cd myapp/sinatra
git pull
</pre></div>
</div>
</div>

<a name='Install%20Globally'></a>
<h3>Install Globally</h3>

<p>You can build the gem on your own:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>git clone git://github.com/sinatra/sinatra.git
cd sinatra
rake sinatra.gemspec
rake install
</pre></div>
</div>
</div>

<p>If you install gems as root, the last step should be:</p>

<div class="highlighter-coderay">
<div class="CodeRay">
  <div class="code"><pre>sudo rake install
</pre></div>
</div>
</div>

<a name='Versioning'></a>
<h2>Versioning</h2>

<p>Sinatra follows <a href="http://semver.org/">Semantic Versioning</a>, both SemVer and
SemVerTag.</p>

<a name='Further%20Reading'></a>
<h2>Further Reading</h2>

<ul>
  <li>
<a href="http://www.sinatrarb.com/">Project Website</a> - Additional documentation,
news, and links to other resources.</li>
  <li>
<a href="http://www.sinatrarb.com/contributing">Contributing</a> - Find a bug? Need
help? Have a patch?</li>
  <li><a href="https://github.com/sinatra/sinatra/issues">Issue tracker</a></li>
  <li><a href="https://twitter.com/sinatra">Twitter</a></li>
  <li><a href="http://groups.google.com/group/sinatrarb/topics">Mailing List</a></li>
  <li>IRC: <a href="irc://chat.freenode.net/#sinatra">#sinatra</a> on http://freenode.net</li>
  <li>
<a href="https://sinatrarb.slack.com">Sinatra &amp; Friends</a> on Slack and see
<a href="https://sinatra-slack.herokuapp.com/">here</a> for an invite.</li>
  <li>
<a href="https://github.com/sinatra/sinatra-book/">Sinatra Book</a> Cookbook Tutorial</li>
  <li>
<a href="http://recipes.sinatrarb.com/">Sinatra Recipes</a> Community
contributed recipes</li>
  <li>API documentation for the <a href="http://www.rubydoc.info/gems/sinatra">latest release</a>
or the <a href="http://www.rubydoc.info/github/sinatra/sinatra">current HEAD</a> on
http://www.rubydoc.info/</li>
  <li><a href="https://travis-ci.org/sinatra/sinatra">CI server</a></li>
</ul>
</body></html>
Something went wrong with that request. Please try again.