scorched 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,35 @@
1
1
  Routing
2
2
  =======
3
3
 
4
- When Scorched receives a request, the first thing it does is iterate over it's internal mapping hash, looking for the any URL pattern that matches the current URL. If it finds an appropriate match, it invokes the ``call`` method on the target defined for that mapping, unless the target is a ``Proc``, in which case it's invoked via ``instance_exec`` to run it within the context of the controller instance.
4
+ When Scorched receives a request, the first thing it does is iterate over it's internal mapping hash, looking for the any URL pattern that matches the current URL. If it finds an appropriate match, it invokes the `call` method on the target defined for that mapping, unless the target is a `Proc`, in which case it's invoked via `instance_exec` to run it within the context of the controller instance.
5
5
 
6
- Mappings can be defined manually using the ``map`` class method, also aliased as ``<<``. Besides the required URL pattern and target elements, a mapping can also define a priority, and one or more conditions. The example below demonstrates the use of all of them.
6
+ Mappings can be defined manually using the `map` class method, also aliased as `<<`. Besides the required URL pattern and target elements, a mapping can also define a priority, and one or more conditions. The example below demonstrates the use of all of them.
7
7
 
8
- # ruby
9
- map pattern: '/', priority: -99, conditions: {methods: ['POST', 'PUT', 'DELETE']}, target: proc { |env|
10
- [200, {}, 'Bugger off']
11
- }
8
+ ```ruby
9
+ map pattern: '/', priority: -99, conditions: {methods: ['POST', 'PUT', 'DELETE']}, target: proc { |env|
10
+ [200, {}, 'Bugger off']
11
+ }
12
+ ```
12
13
 
13
14
  The position the new mapping is inserted into the mapping hash is determined by it's priority, and the priority of the mappings already defined. This avoids re-sorting the mapping hash every time it's added to. This isn't a performance consideration, but is required to maintain the natural insert order of the mappings which have identical priorities (such as the default 0).
14
15
 
15
- A ``mapping`` method is also provided as means to access all defined mappings on a controller, but it should be considered read-only for the reasons just stated.
16
+ A `mapping` method is also provided as means to access all defined mappings on a controller, but it should be considered read-only for the reasons just stated.
16
17
 
17
18
  Route Helpers
18
19
  -------------
19
20
  Adding mappings manually can be a little verbose and painful, which is why Scorched includes a bunch of route helpers which are used in most code examples.
20
21
 
21
- The main route helper which all others delegate to, is the ``route`` class method. Here's what it looks like in both it's simple and advance form:
22
+ The main route helper which all others delegate to, is the `route` class method. Here's what it looks like in both it's simple and advance form:
22
23
 
23
- # ruby
24
- route '/' do
25
- 'Well hello there'
26
- end
27
-
28
- route '/*', 5, methods: ['POST', 'PUT', 'DELETE'] do |capture|
29
- "Hmm trying to change #{capture} I see"
30
- end
24
+ ```ruby
25
+ route '/' do
26
+ 'Well hello there'
27
+ end
28
+
29
+ route '/*', 5, methods: ['POST', 'PUT', 'DELETE'] do |capture|
30
+ "Hmm trying to change #{capture} I see"
31
+ end
32
+ ```
31
33
 
32
34
  You can see pretty clearly how these examples correspond to the pattern, priority, conditions and target options of a manual mapping. The pattern, priority and conditions behave exactly as they do for a manual mapping, with a couple of exceptions.
33
35
 
@@ -35,12 +37,12 @@ The first exception is that the pattern must match to the end of the request pat
35
37
 
36
38
  The other more notable exception is in how the given block is treated. The block given to the route helper is wrapped in another proc. The wrapping proc does a couple of things. It first sends all the captures in the url pattern as argument to the given block, this is shown in the example above. The other thing it does is takes care of assigning the return value to the body of the response.
37
39
 
38
- In the latter of the two examples above, a ``:methods`` condition defines what methods the route is intended to process. The first example has no such condition, so it accepts all HTTP methods. Typically however, a route will handle a single HTTP method, which is why Scorched also provides the convenience helpers: ``get``, ``post``, ``put``, ``delete``, ``head``, ``options``, and ``patch``. These methods automatically define the corresponding ``:method`` condition, with the ``get`` helper also including ``head`` as an accepted HTTP method.
40
+ In the latter of the two examples above, a `:methods` condition defines what methods the route is intended to process. The first example has no such condition, so it accepts all HTTP methods. Typically however, a route will handle a single HTTP method, which is why Scorched also provides the convenience helpers: `get`, `post`, `put`, `delete`, `head`, `options`, and `patch`. These methods automatically define the corresponding `:method` condition, with the `get` helper also including `head` as an accepted HTTP method.
39
41
 
40
42
  Pattern Matching
41
43
  ----------------
42
44
  All patterns attempt to match the remaining unmatched portion of the _request path_; the _request path_ being Rack's
43
- ``path_info`` request variable. The unmatched path will always begin with a forward slash if the previously matched portion of the path ended immediately before, or included as the last character, a forward slash. As an example, if the request was to "/article/21", then both "/article/" => "/21" and "/article" => "/21" would match.
45
+ `path_info` request variable. The unmatched path will always begin with a forward slash if the previously matched portion of the path ended immediately before, or included as the last character, a forward slash. As an example, if the request was to "/article/21", then both "/article/" => "/21" and "/article" => "/21" would match.
44
46
 
45
47
  All patterns must match from the beginning of the path. So even though the pattern "article" would match "/article/21", it wouldn't count as a match because the match didn't start at a non-zero offset.
46
48
 
@@ -55,7 +57,7 @@ String patterns are compiled into Regexp patterns corresponding to the following
55
57
  * `**` - Matches all characters including the forward slash.
56
58
  * `:param` - Same as `*` except the capture is named to whatever the string following the single-colon is.
57
59
  * `::param` - Same as `**` except the capture is named to whatever the string following the double-colon is.
58
- * `$` - If placed at the end of a pattern, the pattern only matches if it matches the entire path. For patterns defined using the route helpers, e.g. ``Controller.route``, ``Controller.get``, this is implied.
60
+ * `$` - If placed at the end of a pattern, the pattern only matches if it matches the entire path. For patterns defined using the route helpers, e.g. `Controller.route`, `Controller.get`, this is implied.
59
61
 
60
62
  ###Regex Patterns
61
63
  Regex patterns offer more power and flexibility than string patterns (naturally). The rules for Regex patterns are identical to String patterns, e.g. they must match from the beginning of the path, etc.
@@ -63,30 +65,31 @@ Regex patterns offer more power and flexibility than string patterns (naturally)
63
65
 
64
66
  Conditions
65
67
  ----------
66
- Conditions are essentially just pre-requisites that must be met before a mapping is invoked to handle the current request. They're implemented as ``Proc`` objects which take a single argument, and return true if the condition is satisfied, or false otherwise. Scorched comes with a number of pre-defined conditions included, many of which are provided by _rack-accept_ - one of the few dependancies of Scorched.
67
-
68
- * ``:charset`` - Character sets accepted by the client.
69
- * ``:encoding`` - Encodings accepted by the client.
70
- * ``:host`` - The host name (i.e. domain name) used in the request.
71
- * ``:language`` - Languages accepted by the client.
72
- * ``:media_type`` - Media types (i.e. content types) accepted by the client.
73
- * ``:methods`` - The request method used, e.g. GET, POST, PUT, ...
74
- * ``:user_agent`` - The user agent string provided with the request. Takes a Regexp or String.
75
- * ``:status`` - The response status of the request. Intended for use by _after_ filters.
76
-
77
- Like configuration options, conditions are implemented using the ``Scorched::Options`` class, so they're inherited and be overridable by child classes. You may easily add your own conditions as the example below demonstrates.
78
-
79
- # ruby
80
- condition[:has_permission] = proc { |v|
81
- user.has_permission == v
82
- }
83
-
84
- get '/', has_permission: true do
85
- 'Welcome'
86
- end
87
-
88
- get '/', has_permission: false do
89
- 'Forbidden'
90
- end
91
-
92
- Each of the built-in conditions can take a single value, or an array of values, with the exception of the ``:host`` and ``:user_agent`` conditions which support Regexp patterns.
68
+ Conditions are essentially just pre-requisites that must be met before a mapping is invoked to handle the current request. They're implemented as `Proc` objects which take a single argument, and return true if the condition is satisfied, or false otherwise. Scorched comes with a number of pre-defined conditions included, many of which are provided by _rack-accept_ - one of the few dependancies of Scorched.
69
+
70
+ * `:charset` - Character sets accepted by the client.
71
+ * `:encoding` - Encodings accepted by the client.
72
+ * `:host` - The host name (i.e. domain name) used in the request.
73
+ * `:language` - Languages accepted by the client.
74
+ * `:media_type` - Media types (i.e. content types) accepted by the client.
75
+ * `:methods` - The request method used, e.g. GET, POST, PUT, ...
76
+ * `:user_agent` - The user agent string provided with the request. Takes a Regexp or String.
77
+ * `:status` - The response status of the request. Intended for use by _after_ filters.
78
+
79
+ Like configuration options, conditions are implemented using the `Scorched::Options` class, so they're inherited and be overridable by child classes. You may easily add your own conditions as the example below demonstrates.
80
+
81
+ ```ruby
82
+ condition[:has_permission] = proc { |v|
83
+ user.has_permission == v
84
+ }
85
+
86
+ get '/', has_permission: true do
87
+ 'Welcome'
88
+ end
89
+
90
+ get '/', has_permission: false do
91
+ 'Forbidden'
92
+ end
93
+ ```
94
+
95
+ Each of the built-in conditions can take a single value, or an array of values, with the exception of the `:host` and `:user_agent` conditions which support Regexp patterns.
@@ -1,30 +1,35 @@
1
1
  Requests and Responses
2
2
  ======================
3
- One of the first things a controller does when it instantiates itself, is make the Rack environment hash accessible via the ``env`` helper, as well as make available a ``Scorched::Request`` and ``Scorched::Response`` object under the respective ``request`` and ``response`` methods.
3
+ One of the first things a controller does when it instantiates itself, is make the Rack environment hash accessible via the `env` helper, as well as make available a `Scorched::Request` and `Scorched::Response` object under the respective `request` and `response` methods.
4
4
 
5
- The ``Scorched::Request`` and ``Scorched::Response`` classes are children of the corresponding _Rack_ request and response classes, with a little extra functionality tacked on.
5
+ The `Scorched::Request` and `Scorched::Response` classes are children of the corresponding _Rack_ request and response classes, with a little extra functionality tacked on.
6
6
 
7
7
  The _request_ object makes accessible all the information associated with the current request, such as the GET and POST data, server and environment information, request headers, and so on. The _response_ is much the same, but in reverse. You'll use the _response_ object to set response headers and manipulate the body of the response.
8
8
 
9
- Refer to the _Rack_ documentation for more information on the ``Rack::Request`` and ``Rack::Response`` classes.
9
+ Refer to the _Rack_ documentation for more information on the `Rack::Request` and `Rack::Response` classes.
10
10
 
11
11
 
12
12
  Scorched Extras
13
13
  ---------------
14
- As mentioned, Scorched tacks a few extras onto it's ``Scorched::Request`` and ``Scorched::Response`` classes. Most of these extras were added as a requirement of the Scorched controller, but they're just as useful to other developers.
14
+ As mentioned, Scorched tacks a few extras onto it's `Scorched::Request` and `Scorched::Response` classes. Most of these extras were added as a requirement of the Scorched controller, but they're just as useful to other developers.
15
15
 
16
- Refer to the generated API documentation for ``Scorched::Request`` and ``Scorched::Response``.
16
+ Refer to the generated API documentation for `Scorched::Request` and `Scorched::Response`.
17
17
 
18
18
 
19
19
  Halting Requests
20
20
  ----------------
21
- There may be instances we're you want to shortcut out-of processing the current request. The ``halt`` method allows you to do this, though it's worth clarifying its behaviour.
21
+ There may be instances we're you want to shortcut out-of processing the current request. The `halt` method allows you to do this, though it's worth clarifying its behaviour.
22
22
 
23
- When ``halt`` is called within a route, it simply exists out of that route, and begins processing any _after_ filters. Halt can also be used within a _before_ or _after_ filter, in which case any remaining filters in the current controller are skipped.
23
+ When `halt` is called within a route, it simply exists out of that route, and begins processing any _after_ filters. Halt can also be used within a _before_ or _after_ filter, in which case any remaining filters in the current controller are skipped.
24
24
 
25
- Calls to ``halt`` don't propagate up the controller chain. They're local to the controller. A call to ``halt`` is equivalent to doing a ``throw :halt``. Calling ``halt`` is often preferred though because as well as being shorter, it can take an optional argument to set the response status, which is something you typically want to do when halting a request.
25
+ Calls to `halt` don't propagate up the controller chain. They're local to the controller. A call to `halt` is equivalent to doing a `throw :halt`. Calling `halt` is often preferred though because as well as being shorter, it can take an optional argument to set the response status, which is something you typically want to do when halting a request.
26
+
27
+
28
+ Passing Requests
29
+ ----------------
30
+ A route may _pass_ a request to the next matching route. _passing_ is very similar to halting, except an opportunity is given to other matching routes to fulfil the request. This is implemented as a throw/catch mechanism, much the same as `halt`. You can do a `throw :pass` manually, or use the helper method `pass`.
26
31
 
27
32
 
28
33
  Redirections
29
34
  ------------
30
- A common requirement of many applications is to redirect requests to another URL based on some kind of condition. Scorched offers the very simple ``redirect`` method which takes one argument - the URL to redirect to. Like ``halt`` it's mostly a convenience method. It sets the _Location_ header of the response before halting the request.
35
+ A common requirement of many applications is to redirect requests to another URL based on some kind of condition. Scorched offers the very simple `redirect` method which takes one argument - the URL to redirect to. Like `halt` it's mostly a convenience method. It sets the _Location_ header of the response before halting the request.
@@ -1,6 +1,6 @@
1
1
  Filters
2
2
  =======
3
- Filters serve as a handy place to put functionality and behaviour that's common to a set of routes, or for that matter, a whole website or application. Filters are executed in the context of the controller; the same context as routes. Filters are also inheritable, meaning sub-classes inherit the filters of their parent - this inheritance is enabled through the use of the ``Scorched::Collection`` class, and is implemented such that each filter will only run once per-request.
3
+ Filters serve as a handy place to put functionality and behaviour that's common to a set of routes, or for that matter, a whole website or application. Filters are executed in the context of the controller; the same context as routes. Filters are also inheritable, meaning sub-classes inherit the filters of their parent - this inheritance is enabled through the use of the `Scorched::Collection` class, and is implemented such that each filter will only run once per-request.
4
4
 
5
5
  There are currently two types of filter in Scorched, both of which are documented below.
6
6
 
@@ -9,24 +9,27 @@ Before and After Filters
9
9
  ------------------------
10
10
  Before and After filters allow pre- and post-processing of requests. They are executed before and after each request, respectively.
11
11
 
12
- # ruby
13
- before do
14
- raise Error, "Must be logged in to access this site" unless session[:logged_in] == true
15
- end
12
+ ```ruby
13
+ before do
14
+ raise Error, "Must be logged in to access this site" unless session[:logged_in] == true
15
+ end
16
+ ```
16
17
 
17
18
  Like routes, filters can have conditions defined on them, for example:
18
19
 
19
- # ruby
20
- after media_type: 'application/json' do
21
- response.body.to_json!
22
- end
20
+ ```ruby
21
+ after media_type: 'application/json' do
22
+ response.body.to_json!
23
+ end
24
+ ```
23
25
 
24
26
  Before and after filters run even if no route within the controller matches. This makes them suitable for handling 404 errors for example.
25
27
 
26
- # ruby
27
- after status: 404 do
28
- response.body = render(:not_found)
29
- end
28
+ ```ruby
29
+ after status: 404 do
30
+ response.body = render(:not_found)
31
+ end
32
+ ```
30
33
 
31
34
  Your imagination is the only limitation.
32
35
 
@@ -39,7 +42,8 @@ Error filters can handle exceptions raised from within the request target, as we
39
42
 
40
43
  Error filters can target only specific types of exception class, in much the same way as a typical Ruby rescue block.
41
44
 
42
- # ruby
43
- error PermissionError do |e|
44
- flash[:error] = "You do not have the appropriate permission to perform that action: #{e.message}"
45
- end
45
+ ```ruby
46
+ error PermissionError do |e|
47
+ flash[:error] = "You do not have the appropriate permission to perform that action: #{e.message}"
48
+ end
49
+ ```
@@ -3,16 +3,17 @@ Middleware
3
3
 
4
4
  While middleware can be added in your _rackup_ file to wrap your Scorched application, it can be more desirable to add the middleware at the controller level. Scorched itself requires this as it needs to include a set of Rack middleware out-of-the-box. Developers can't be expected to manually add these to their rackup file for every Scorched application.
5
5
 
6
- Like _filters_, middleware is inheritable thanks to it's use of the ``Scorched::Collection`` class. Also like _filters_, middleware proc's are only run once per request, which prevents unintended double-loading of middleware.
6
+ Like _filters_, middleware is inheritable thanks to it's use of the `Scorched::Collection` class. Also like _filters_, middleware proc's are only run once per request, which prevents unintended double-loading of middleware.
7
7
 
8
- Adding middleware to a Scorched controller involves pushing a proc onto the end of the middleware collection, accessible via the ``middleware`` accessor method. The given proc is ``instance_exec``'d in the context of a Rack builder object, and so can be used for more than just loading middleware.
8
+ Adding middleware to a Scorched controller involves pushing a proc onto the end of the middleware collection, accessible via the `middleware` accessor method. The given proc is `instance_exec`'d in the context of a Rack builder object, and so can be used for more than just loading middleware.
9
9
 
10
- # ruby
11
- middleware << proc do
12
- use Rack::Session::Cookie, secret: 'blah'
13
- # Stolen from Rack's own documentation...
14
- map "/lobster" do
15
- use Rack::Lint
16
- run Rack::Lobster.new
17
- end
18
- end
10
+ ```ruby
11
+ middleware << proc do
12
+ use Rack::Session::Cookie, secret: 'blah'
13
+ # Stolen from Rack's own documentation...
14
+ map "/lobster" do
15
+ use Rack::Lint
16
+ run Rack::Lobster.new
17
+ end
18
+ end
19
+ ```
@@ -3,87 +3,93 @@ Request and Session Data
3
3
 
4
4
  GET and POST Data
5
5
  -----------------
6
- Many ruby frameworks provide helpers for accessing GET and POST data via some kind of generic accessor, such as a ``params`` method, but Rack already provides this functionality out of the box, and more.
7
-
8
- # ruby
9
- post '/' do
10
- request.GET['view'] # Key/value pairs submitted in the query string of the URL.
11
- request.POST['username'] # Key/value pairs submitted in the payload of a POST request.
12
- request.params[:username] # A merged hash of GET and POST data.
13
- request[:username] # Shortcut to merged hash of GET and POST data.
14
- end
6
+ Many ruby frameworks provide helpers for accessing GET and POST data via some kind of generic accessor, such as a `params` method, but Rack already provides this functionality out of the box, and more.
7
+
8
+ ```ruby
9
+ post '/' do
10
+ request.GET['view'] # Key/value pairs submitted in the query string of the URL.
11
+ request.POST['username'] # Key/value pairs submitted in the payload of a POST request.
12
+ request.params[:username] # A merged hash of GET and POST data.
13
+ request[:username] # Shortcut to merged hash of GET and POST data.
14
+ end
15
+ ```
15
16
 
16
17
  Uploaded files are also accessible as ordinary fields, except the associated value is a hash of properties, instead of a string. An example of an application that accepts file uploads is included in the "examples" directory of the Scorched git repository.
17
18
 
18
19
  Cookies
19
20
  -------
20
- While Rack provides a relatively simple means of setting, retrieving and deleting cookies, Scorched aggregates those three actions into a single method, ``cookie``, for the sake of brevity and simplicity.
21
+ While Rack provides a relatively simple means of setting, retrieving and deleting cookies, Scorched aggregates those three actions into a single method, `cookie`, for the sake of brevity and simplicity.
21
22
 
22
- # ruby
23
- def '/' do
24
- cookie :previous_page # Retrieves the cookie.
25
- cookie :previous_page, '/search' # Sets the cookie
26
- cookie :previous_page, nil # Deletes the cookies
27
- end
23
+ ```ruby
24
+ def '/' do
25
+ cookie :previous_page # Retrieves the cookie.
26
+ cookie :previous_page, '/search' # Sets the cookie
27
+ cookie :previous_page, nil # Deletes the cookies
28
+ end
29
+ ```
28
30
 
29
- For each of the above lines, the corresponding Rack methods are called, e.g. ``Rack::Requeste#cookies``, ``Rack::Response#set_cookie`` and ``Rack::Response#delete_cookie``. The values for setting and deleting a cookie can also be a hash, like ``set_cookie`` and ``delete_cookie`` can take directly. Deleting still works when a Hash is provided, as long as the ``value`` property is nil.
31
+ For each of the above lines, the corresponding Rack methods are called, e.g. `Rack::Requeste#cookies`, `Rack::Response#set_cookie` and `Rack::Response#delete_cookie`. The values for setting and deleting a cookie can also be a hash, like `set_cookie` and `delete_cookie` can take directly. Deleting still works when a Hash is provided, as long as the `value` property is nil.
30
32
 
31
- # ruby
32
- def '/' do
33
- cookie :view, path: '/account', value: 'datasheet' # Sets the cookie
34
- cookie :view, path: '/account', value: nil # Deletes the cookie
35
- end
33
+ ```ruby
34
+ def '/' do
35
+ cookie :view, path: '/account', value: 'datasheet' # Sets the cookie
36
+ cookie :view, path: '/account', value: nil # Deletes the cookie
37
+ end
38
+ ```
36
39
 
37
40
 
38
41
  Sessions
39
42
  --------
40
- Sessions are completely handled by Rack. For conveniance, a ``session`` helper is provided. This merely acts as an alias to ``request['rack.session']`` however. It was raise an exception if called without any Rack session middleware loaded, such as ``Rack::Session::Cookie``.
41
-
42
- # ruby
43
- class App < Scorched::Controller
44
- middleware << proc {
45
- use Rack::Session::Cookie, secret: 'blah'
46
- }
47
-
48
- get '/' do
49
- session['logged_in'] ? 'You're currently logged in.' : 'Please login.'
50
- end
51
- end
43
+ Sessions are completely handled by Rack. For conveniance, a `session` helper is provided. This merely acts as an alias to `request['rack.session']` however. It was raise an exception if called without any Rack session middleware loaded, such as `Rack::Session::Cookie`.
44
+
45
+ ```ruby
46
+ class App < Scorched::Controller
47
+ middleware << proc {
48
+ use Rack::Session::Cookie, secret: 'blah'
49
+ }
50
+
51
+ get '/' do
52
+ session['logged_in'] ? 'You're currently logged in.' : 'Please login.'
53
+ end
54
+ end
55
+ ```
52
56
 
53
57
  ###Flash
54
58
  A common requirement for websites, and especially web applications, is to provide a message on the next page load corresponding to an action that a user just performed. A common framework idiom that Scorched happily implements are flash session variables - special session data that lives for only a single page load.
55
59
 
56
60
  This isn't as trivial to implement as it may sound at a glance, which is why Scorched provides this helper.
57
61
 
58
- # ruby
59
- get '/' do
60
- "<span class="success">#{flash[:success]}</span>" if flash[:success]
61
- end
62
-
63
- post '/login' do
64
- flash[:success] = 'Logged in successfully.'
65
- end
62
+ ```ruby
63
+ get '/' do
64
+ "<span class="success">#{flash[:success]}</span>" if flash[:success]
65
+ end
66
+
67
+ post '/login' do
68
+ flash[:success] = 'Logged in successfully.'
69
+ end
70
+ ```
66
71
 
67
72
  The flash helper allows multiple sets of flash session data to be stored under different names. Because of how flash sessions are implemented, they're only deleted on the next page load if that particular flash data set is accessed. These properties of flash sessions can satisfy some interesting use cases. Here's a very uninteresting example:
68
73
 
69
- # ruby
70
- class App < Scorched::Controller
71
- get '/' do
72
- "<span class="success">#{flash[:success]}</span>" if flash[:success]
73
- end
74
-
75
- post '/login' do
76
- flash[:success] = 'Logged in successfully.'
77
- if user.membership_type == 'vip' && user.membership_expiry < 5
78
- flash(:vip)[:warning] = 'Your VIP membership is about to expire, please renew it.'
79
- end
80
- end
81
-
82
- controller pattern: '/vip' do
83
- get '/' do
84
- "<span class="warning">#{flash(:vip)[:warning]}</span>" if flash(:vip)[:warning]
85
- end
86
- end
74
+ ```ruby
75
+ class App < Scorched::Controller
76
+ get '/' do
77
+ "<span class="success">#{flash[:success]}</span>" if flash[:success]
78
+ end
79
+
80
+ post '/login' do
81
+ flash[:success] = 'Logged in successfully.'
82
+ if user.membership_type == 'vip' && user.membership_expiry < 5
83
+ flash(:vip)[:warning] = 'Your VIP membership is about to expire, please renew it.'
84
+ end
85
+ end
86
+
87
+ controller '/vip' do
88
+ get '/' do
89
+ "<span class="warning">#{flash(:vip)[:warning]}</span>" if flash(:vip)[:warning]
87
90
  end
91
+ end
92
+ end
93
+ ```
88
94
 
89
- In the rather contrived example above, when a VIP user logs in, a message is generated and stored as a flash session variable in the ``:vip`` flash data set. Because the ``:vip`` flash data set isn't accessed on the main page, it lives on until it's finally re-accessed on the VIP page.
95
+ In the rather contrived example above, when a VIP user logs in, a message is generated and stored as a flash session variable in the `:vip` flash data set. Because the `:vip` flash data set isn't accessed on the main page, it lives on until it's finally re-accessed on the VIP page.
@@ -1,22 +1,22 @@
1
1
  Views
2
2
  =====
3
3
 
4
- Scorched uses Tilt to render templates in various supported formats. Because of this, views have been implemented as a single method, ``render``.
4
+ Scorched uses Tilt to render templates in various supported formats. Because of this, views have been implemented as a single method, `render`.
5
5
 
6
- ``render`` can take a file path or a string as the template. If a symbol is given, it assumes it's a file path, were as a string is assumed to be the template markup. ``render`` can also takes a set of options.
6
+ `render` can take a file path or a string as the template. If a symbol is given, it assumes it's a file path, were as a string is assumed to be the template markup. `render` can also takes a set of options.
7
7
 
8
- * ``:dir`` - The directory containing the views. This can be absolute or relative to the current working directory.
9
- * ``:layout`` - The template to render _around_ the view.
10
- * ``:engine`` - The template engine to use if it can't be derived from the file name. This is always required for string templates. Can be any string or symbol that Tilt recognises, e.g :erb, :haml, :sass, etc.
8
+ * `:dir` - The directory containing the views. This can be absolute or relative to the current working directory.
9
+ * `:layout` - The template to render _around_ the view.
10
+ * `:engine` - The template engine to use if it can't be derived from the file name. This is always required for string templates. Can be any string or symbol that Tilt recognises, e.g :erb, :haml, :sass, etc.
11
11
 
12
- Any unrecognised options are passed through to Tilt and the corresponding rendering engine. A common example being the ``:locals`` option, used to set local variables made available within the scope of the view.
12
+ Any unrecognised options are passed through to Tilt and the corresponding rendering engine. A common example being the `:locals` option, used to set local variables made available within the scope of the view.
13
13
 
14
- Finally, ``render`` takes an optional block to be _yielded_ within the view being rendered, if supported by the rendering engine. Layout's use this feature.
14
+ Finally, `render` takes an optional block to be _yielded_ within the view being rendered, if supported by the rendering engine. Layout's use this feature.
15
15
 
16
16
  Layouts
17
17
  -------
18
- When a layout is given, a subsequent call to ``render`` is made, with the rendered result of the main template given as the block to be yielded. The defined ``:layout`` is provided as the first argument on the sub-sequent call to ``render``, so the same rules apply. Layouts inherit the options of the main template being rendered.
18
+ When a layout is given, a subsequent call to `render` is made, with the rendered result of the main template given as the block to be yielded. The defined `:layout` is provided as the first argument on the sub-sequent call to `render`, so the same rules apply. Layouts inherit the options of the main template being rendered.
19
19
 
20
20
  Partials
21
21
  --------
22
- There are cases where a you may want a view to be composed of more than just a layout and a single view. The view may contain one or more sub-views, commonly referred to as partials. Scorched makes provisions for this by ignoring the default layout when ``render`` is called within a view, hence negating the requirement to explicitly override the layout.
22
+ There are cases where a you may want a view to be composed of more than just a layout and a single view. The view may contain one or more sub-views, commonly referred to as partials. Scorched makes provisions for this by ignoring the default layout when `render` is called within a view, hence negating the requirement to explicitly override the layout.