Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
winton committed Mar 12, 2011
1 parent 1ea3366 commit f50ec06
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 43 deletions.
91 changes: 50 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Stasis uses [Tilt](https://github.com/rtomayko/tilt) to support the following te
Example
-------

Our [spec project](https://github.com/winton/stasis/tree/master/spec/fixtures/project) implements all the features below.

Get Started
-----------

Create a directory for your project and a markup file:

### view.erb
Expand All @@ -63,10 +68,21 @@ You now have a `public` directory with rendered markup:
Controllers
-----------

A controller defines `before` and `after` render callbacks.

The only reserved filename in a Stasis project is `controller.rb`.

You can have a `controller.rb` at any directory level:

controller.rb
index.erb
pages/
controller.rb
page.erb

Callbacks
---------

Define `before` and `after` render callbacks within your controller:

### controller.rb

# Call before any file renders
Expand All @@ -85,19 +101,16 @@ The only reserved filename in a Stasis project is `controller.rb`.

Welcome to <%= @title %>!

### Multiple Controllers

You can have a `controller.rb` at any directory level:
Change the Destination
----------------------

controller.rb
index.erb
pages/
controller.rb
page.erb
Let's say we want `view.erb` to be our front page:

This allows you to better organize your callbacks.
### controller.rb

Read more about [callback execution order](https://github.com/winton/stasis/wiki/Callback-Execution-Order).
before 'view.erb' do
@destination = '/index.html'
end

Layouts
-------
Expand All @@ -121,25 +134,38 @@ Create the layout markup:
end

before 'layout.erb' do
@ignore = true
@destination = nil
end

We want `view.erb` to use the layout, so we set `@layout = 'layout.erb'`.

We do not want a `public/layout.html` file, so we set `@ignore = true`.
We do not want a `public/layout.html` file, so we set `@destination = nil`.

Change the Path
---------------
Helpers
-------

Let's say we want `view.erb` to be our front page.
Define helper methods within your controllers.

### controller.rb

before 'view.erb' do
@path = '/'
helpers do
def active?(path)
@source == path
end
end

Adding `@path = '/'` changes the render location to `public/index.html`.
### layout.erb

<%= active?('view.erb') %>

Class Variables
---------------

To summarize, the following class variables have a special purpose in a Stasis project:

* `@destination` - Get/set the destination path within `public/`
* `@layout` - Get/set a file path to use as the layout
* `@source` - Get/set the file path that is being rendered

Continuous Rendering
--------------------
Expand All @@ -159,25 +185,8 @@ In your browser, visit [http://localhost:3000](http://localhost:3000).

In web server mode, Stasis continuously renders (`-c`).

Programmatically Generate Content
---------------------------------

require 'rubygems'
require 'stasis'

stasis = Stasis.new '/path/to/project'

# Generate all files
stasis.generate '**/*'

# Generate one file
stasis.generate 'view.erb'

# Generate one file with extra callbacks
stasis.generate 'view.erb' do
before do
@path = "/custom/path"
end
end
Other Topics:
-------------

Extra callbacks execute before any filters defined in the project.
* [Callback Execution Order](https://github.com/winton/stasis/wiki/Callback-Execution-Order).
* [Run Stasis Programmatically](https://github.com/winton/stasis/wiki/Run-Stasis-Programmatically)
17 changes: 16 additions & 1 deletion lib/stasis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def generate(*paths)
next unless File.file?(path)
next unless Tilt.mappings.keys.include?(File.extname(path)[1..-1])
context = Context::Render.new rel_path
trigger :helpers, context, path, rel_path
trigger :before, context, path, rel_path
next if context.ignore
template = Tilt.new path
Expand All @@ -56,7 +57,11 @@ def trigger(type, context, path, rel_path)
blocks += callbacks.send(type, rel_path)
blocks += callbacks.send(type, File.basename(rel_path))
blocks.each do |block|
context.instance_eval &block
if type == :helpers
context.class.class_eval &block
else
context.instance_eval &block
end
end
end
dir = File.expand_path('../', dir)
Expand Down Expand Up @@ -85,6 +90,16 @@ def before(view=nil, &block)
@before[view]
end
end

def helpers(view=nil, &block)
@helpers ||= {}
@helpers[view] ||= []
if block
@helpers[view] << block
else
@helpers[view]
end
end
end

class Render
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/project/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
before 'view.erb' do
@layout = 'layout.erb'
@title = 'My Site'
end

helpers do
def blah
'!!!'
end
end
2 changes: 1 addition & 1 deletion spec/fixtures/project/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<head>
<title><%= @title %></title>
</head>
<body><%= yield %></body>
<body><%= yield %><%= blah %></body>
</html>

0 comments on commit f50ec06

Please sign in to comment.