View Helpers

If you drop a file called "view_helpers.rb" in your views directory, you can define custom helpers for your Haml and ERB views. Just create a ViewHelpers module and define your helper methods there:

module ViewHelpers
  def custom_method
    "Request object: #{request.headers['user-agent']}"
  end
end

Helpers have full access to the request and response objects so you can easily read and manipulate headers or do other fancy tricks.

Serve provides a number of stock helpers to make it easier for you to prototype Rails applications:

# Escape Helpers
html_escape(string)                  # escape HTML in string
h(string)                            # alias for html_escape
json_escape(string)                  # escape invalid JSON characters in string
j(string)                            # alias for json_escape

# Content Helpers
capture(&block)                      # Capture a block inside of a view or layout
content_for(symbol, &block)          # Capture a block and store it by symbol
content_for?(symbol)                 # Has content been captured for symbol?
get_content_for(symbol)              # Retrieve content for symbol (equivalent to `yield symbol`)
set_content_for(symbol, string)      # Set content for symbol to string

# Flash Helpers
flash                                # fake flash for request

# Param Helpers
params                               # access params for request
boolean_param(key)                   # retrieve value of a boolean param (true/false)

# Render Helpers
render(partial)                      # Render a partial

# Tag Helpers
content_tag(name, content, ...)      # a double HTML tag
tag(name, ...)                       # a single HTML tag
image_tag(src, ...)                  # an HTML image tag
image(name, ...)                     # shorthand for standard image tag
javascript_tag(...)                  # a javascript tag
link_to(name, href, ...)             # a link to name at href
link_to_function(name, ...)          # a link to a function
mail_to(email_address, ...)          # a mailto link
javascript_include_tag(*sources)     # Rails-style javascript tags for sources
stylesheet_link_tag(*sources)        # Rails-style stylesheet tags for sources

For more information, see: github.com/jlong/serve/blob/master/lib/serve/view_helpers.rb