# Sinatra
[![Gem Version](https://badge.fury.io/rb/sinatra.svg)](https://badge.fury.io/rb/sinatra)
[![Build Status](https://secure.travis-ci.org/sinatra/sinatra.svg)](https://travis-ci.org/sinatra/sinatra)
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)
Sinatra is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
quickly creating web applications in Ruby with minimal effort:
```ruby
# myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
```
Install the gem:
```shell
gem install sinatra
```
And run with:
```shell
ruby myapp.rb
```
View at: [http://localhost:4567](http://localhost:4567)
The code you changed will not take effect until you restart the server.
Please restart the server every time you change or use
[sinatra/reloader](http://www.sinatrarb.com/contrib/reloader).
It is recommended to also run `gem install puma`, which Sinatra will
pick up if available.
## Table of Contents
* [Sinatra](#sinatra)
* [Table of Contents](#table-of-contents)
* [Routes](#routes)
* [Conditions](#conditions)
* [Return Values](#return-values)
* [Custom Route Matchers](#custom-route-matchers)
* [Static Files](#static-files)
* [Views / Templates](#views--templates)
* [Literal Templates](#literal-templates)
* [Available Template Languages](#available-template-languages)
* [Haml Templates](#haml-templates)
* [Erb Templates](#erb-templates)
* [Builder Templates](#builder-templates)
* [Nokogiri Templates](#nokogiri-templates)
* [Sass Templates](#sass-templates)
* [SCSS Templates](#scss-templates)
* [Less Templates](#less-templates)
* [Liquid Templates](#liquid-templates)
* [Markdown Templates](#markdown-templates)
* [Textile Templates](#textile-templates)
* [RDoc Templates](#rdoc-templates)
* [AsciiDoc Templates](#asciidoc-templates)
* [Radius Templates](#radius-templates)
* [Markaby Templates](#markaby-templates)
* [RABL Templates](#rabl-templates)
* [Slim Templates](#slim-templates)
* [Creole Templates](#creole-templates)
* [MediaWiki Templates](#mediawiki-templates)
* [CoffeeScript Templates](#coffeescript-templates)
* [Stylus Templates](#stylus-templates)
* [Yajl Templates](#yajl-templates)
* [WLang Templates](#wlang-templates)
* [Accessing Variables in Templates](#accessing-variables-in-templates)
* [Templates with `yield` and nested layouts](#templates-with-yield-and-nested-layouts)
* [Inline Templates](#inline-templates)
* [Named Templates](#named-templates)
* [Associating File Extensions](#associating-file-extensions)
* [Adding Your Own Template Engine](#adding-your-own-template-engine)
* [Using Custom Logic for Template Lookup](#using-custom-logic-for-template-lookup)
* [Filters](#filters)
* [Helpers](#helpers)
* [Using Sessions](#using-sessions)
* [Session Secret Security](#session-secret-security)
* [Session Config](#session-config)
* [Choosing Your Own Session Middleware](#choosing-your-own-session-middleware)
* [Halting](#halting)
* [Passing](#passing)
* [Triggering Another Route](#triggering-another-route)
* [Setting Body, Status Code and Headers](#setting-body-status-code-and-headers)
* [Streaming Responses](#streaming-responses)
* [Logging](#logging)
* [Mime Types](#mime-types)
* [Generating URLs](#generating-urls)
* [Browser Redirect](#browser-redirect)
* [Cache Control](#cache-control)
* [Sending Files](#sending-files)
* [Accessing the Request Object](#accessing-the-request-object)
* [Attachments](#attachments)
* [Dealing with Date and Time](#dealing-with-date-and-time)
* [Looking Up Template Files](#looking-up-template-files)
* [Configuration](#configuration)
* [Configuring attack protection](#configuring-attack-protection)
* [Available Settings](#available-settings)
* [Environments](#environments)
* [Error Handling](#error-handling)
* [Not Found](#not-found)
* [Error](#error)
* [Rack Middleware](#rack-middleware)
* [Testing](#testing)
* [Sinatra::Base - Middleware, Libraries, and Modular Apps](#sinatrabase---middleware-libraries-and-modular-apps)
* [Modular vs. Classic Style](#modular-vs-classic-style)
* [Serving a Modular Application](#serving-a-modular-application)
* [Using a Classic Style Application with a config.ru](#using-a-classic-style-application-with-a-configru)
* [When to use a config.ru?](#when-to-use-a-configru)
* [Using Sinatra as Middleware](#using-sinatra-as-middleware)
* [Dynamic Application Creation](#dynamic-application-creation)
* [Scopes and Binding](#scopes-and-binding)
* [Application/Class Scope](#applicationclass-scope)
* [Request/Instance Scope](#requestinstance-scope)
* [Delegation Scope](#delegation-scope)
* [Command Line](#command-line)
* [Multi-threading](#multi-threading)
* [Requirement](#requirement)
* [The Bleeding Edge](#the-bleeding-edge)
* [With Bundler](#with-bundler)
* [Versioning](#versioning)
* [Further Reading](#further-reading)
## Routes
In Sinatra, a route is an HTTP method paired with a URL-matching pattern.
Each route is associated with a block:
```ruby
get '/' do
.. show something ..
end
post '/' do
.. create something ..
end
put '/' do
.. replace something ..
end
patch '/' do
.. modify something ..
end
delete '/' do
.. annihilate something ..
end
options '/' do
.. appease something ..
end
link '/' do
.. affiliate something ..
end
unlink '/' do
.. separate something ..
end
```
Routes are matched in the order they are defined. The first route that
matches the request is invoked.
Routes with trailing slashes are different from the ones without:
```ruby
get '/foo' do
# Does not match "GET /foo/"
end
```
Route patterns may include named parameters, accessible via the
`params` hash:
```ruby
get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params['name'] is 'foo' or 'bar'
"Hello #{params['name']}!"
end
```
You can also access named parameters via block parameters:
```ruby
get '/hello/:name' do |n|
# matches "GET /hello/foo" and "GET /hello/bar"
# params['name'] is 'foo' or 'bar'
# n stores params['name']
"Hello #{n}!"
end
```
Route patterns may also include splat (or wildcard) parameters, accessible
via the `params['splat']` array:
```ruby
get '/say/*/to/*' do
# matches /say/hello/to/world
params['splat'] # => ["hello", "world"]
end
get '/download/*.*' do
# matches /download/path/to/file.xml
params['splat'] # => ["path/to/file", "xml"]
end
```
Or with block parameters:
```ruby
get '/download/*.*' do |path, ext|
[path, ext] # => ["path/to/file", "xml"]
end
```
Route matching with Regular Expressions:
```ruby
get /\/hello\/([\w]+)/ do
"Hello, #{params['captures'].first}!"
end
```
Or with a block parameter:
```ruby
get %r{/hello/([\w]+)} do |c|
# Matches "GET /meta/hello/world", "GET /hello/world/1234" etc.
"Hello, #{c}!"
end
```
Route patterns may have optional parameters:
```ruby
get '/posts/:format?' do
# matches "GET /posts/" and any extension "GET /posts/json", "GET /posts/xml" etc
end
```
Routes may also utilize query parameters:
```ruby
get '/posts' do
# matches "GET /posts?title=foo&author=bar"
title = params['title']
author = params['author']
# uses title and author vari
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
最新版windows logstash-8.10.0-windows-x86_64.zip最新版windows logstash-8.10.0-windows-x86_64.zip最新版windows logstash-8.10.0-windows-x86_64.zip
资源推荐
资源详情
资源评论
收起资源包目录
最新版windows logstash-8.10.0-windows-x86-64.zip (2000个子文件)
xml_node.c 65KB
xml_document.c 20KB
xml_reader.c 19KB
gumbo.c 18KB
xml_xpath_context.c 12KB
xml_node_set.c 12KB
xslt_stylesheet.c 11KB
xml_sax_parser.c 9KB
nokogiri.c 9KB
html4_element_description.c 7KB
xml_sax_parser_context.c 7KB
xml_schema.c 7KB
xml_namespace.c 5KB
html4_document.c 5KB
xml_dtd.c 4KB
xml_relax_ng.c 4KB
xml_sax_push_parser.c 4KB
atomic_reference.c 4KB
xml_element_content.c 3KB
api.c 3KB
libxml2_backwards_compat.c 3KB
html4_sax_parser_context.c 3KB
xml_entity_decl.c 3KB
xml_encoding_handler.c 3KB
html4_sax_push_parser.c 2KB
xml_syntax_error.c 2KB
xml_attr.c 2KB
xml_cdata.c 2KB
xml_text.c 2KB
xml_comment.c 2KB
xml_element_decl.c 1KB
xml_attribute_decl.c 1KB
xml_processing_instruction.c 1KB
test_global_handlers.c 1KB
xml_entity_reference.c 1KB
html4_entity_lookup.c 1KB
xml_document_fragment.c 944B
main.c 103B
b.c 65B
a.c 65B
rdoc.css 7KB
jvmti.h 82KB
AccessBridgePackages.h 76KB
jni.h 74KB
AccessBridgeCalls.h 35KB
classfile_constants.h 22KB
snappy-test.h 17KB
snappy-stubs-internal.h 15KB
jawt.h 12KB
snappy.h 8KB
jdwpTransport.h 8KB
AccessBridgeCallbacks.h 5KB
snappy-internal.h 5KB
snappy-c.h 5KB
snappy-sinksource.h 5KB
jvmticmlr.h 5KB
jawt_md.h 2KB
jni_md.h 2KB
XmlNode.java 59KB
DOM2DTM.java 56KB
DOM2DTMdefaultNamespaceDeclarationNode.java 26KB
NokogiriService.java 25KB
NokogiriHelpers.java 24KB
SaveContextVisitor.java 23KB
XmlDocument.java 22KB
Canonicalizer11.java 21KB
CanonicalizerBase.java 18KB
XmlReader.java 17KB
XMLUtils.java 15KB
XmlDtd.java 15KB
ReaderNode.java 14KB
Canonicalizer20010315.java 12KB
XmlSchema.java 12KB
XsltStylesheet.java 11KB
XmlNodeSet.java 11KB
NameSpaceSymbTable.java 10KB
Canonicalizer20010315Excl.java 10KB
XmlXpathContext.java 10KB
XmlSaxParserContext.java 9KB
XmlElementContent.java 9KB
NokogiriHandler.java 9KB
Html4SaxParserContext.java 9KB
XmlDomParserContext.java 8KB
Canonicalizer.java 8KB
ElementProxy.java 8KB
HtmlDomParserContext.java 8KB
XmlSaxPushParser.java 7KB
ParserContext.java 7KB
XalanDTMManagerPatch.java 7KB
AtomicReferenceLibrary.java 7KB
Html4SaxPushParser.java 6KB
CanonicalizerPhysical.java 6KB
XmlDocumentFragment.java 5KB
XmlNamespace.java 5KB
NokogiriNamespaceCache.java 5KB
InclusiveNamespaces.java 5KB
Html4Document.java 5KB
NokogiriXPathFunction.java 5KB
C14nHelper.java 5KB
UtfHelpper.java 4KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
h735004790
- 粉丝: 33
- 资源: 1278
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功