= sinatra_more
== Introduction
(Not quite ready to be used yet!)
This will be a plugin which expand sinatra's capabilities in many ways.
Note that certain template specific helpers are known to work with haml, erb, and erubis
Let me expand briefly on what I want to accomplish with this gem. I love sinatra but if I want to use it
for any non-trivial application I very quickly miss a lot of the extra tools provided by rails.
Now the obvious question is, why not just use rails? Well, that would be the easy answer. However, until
version 3 comes along, Rails is quite a large framework and I love the spirit of sinatra which acts
as a thin wrapper on top of rack allowing middleware to do most of the work and pulling in complexity
only as needed.
My goal with this extension is to maintain the spirit of Sinatra and at the same time create a standard library
of tools, helpers and components that will make Sinatra scale to allow for extremely complex apps.
Here is a small list of what sinatra_more might contain:
* Lots of generic view helpers (tag, content_tag, ...)
* Asset helpers (link_to, image_tag, javascript_include_tag, ...)
* Form helpers and form builder support (form_tag, form_for, text_field, ...)
* Plug and play components for Warden (authentication)
Keep in mind, the user will be able to pull in components as necessary and leave out any that are not required.
Obviously the project isn't ready for primetime yet but please help me brainstorm and
fork the project if you have any ideas that will help flesh out this project!
== Usage
This extension can be easily registered into any existing sinatra application. You can require
different components based on which pieces are useful for your application.
# app.rb
require 'sinatra/base'
require 'sinatra_more'
class Application < Sinatra::Base
register SinatraMore::MarkupPlugin
register SinatraMore::RenderPlugin
register SinatraMore::WardenPlugin
end
This will then allow you to use whichever components that have been registered. A breakdown is below:
=== MarkupPlugin
This component provides a great deal of view helpers related to html markup generation.
There are helpers for generating tags, forms, links, images, and more. Most of the basic
methods should be very familiar to anyone who has used rails view helpers.
==== Output Helpers
* capture_html(*args, &block)
* Captures the html from a block of template code for erb or haml
* capture_html(&block) => "...html..."
* concat_content(text="")
* Outputs the given text to the templates buffer directly in erb or haml
* concat_content("This will be output to the template buffer in erb or haml")
==== Tag Helpers
* tag(name, options={})
* Creates an html tag with the given name and options
* tag(:br, :style => 'clear:both') =>
* tag(:p, :content => "demo", :class => 'large') =>
demo
* content_tag(name, content, options={}) * Creates an html tag with given name, content and options * content_tag(:p, "demo", :class => 'light') =>demo
* content_tag(:p, :class => 'dark') { ...content... } =>...content...
* input_tag(type, options = {}) * Creates an html input field with given type and options * input_tag :text, :class => "demo" * input_tag :password, :value => "secret", :class => "demo" ==== Asset Helpers * flash_tag(kind, options={}) * Creates a div to display the flash of given type if it exists * flash_tag(:notice, :class => 'flash', :id => 'flash-notice') * link_to(*args, &block) * Creates a link element with given name, url and options * link_to 'click me', '/dashboard', :class => 'linky' * link_to('/dashboard', :class => 'blocky') { ...content... } * image_tag(url, options={}) * Creates an image element with given url and options * image_tag('icons/avatar.png') * stylesheet_link_tag(*sources) * Returns a stylesheet link tag for the sources specified as arguments * stylesheet_link_tag 'style', 'application', 'layout' * javascript_include_tag(*sources) * Returns an html script tag for each of the sources provided. * javascript_include_tag 'application', 'special' ==== Form Helpers * form_for(object, url, settings={}, &block) * form_tag(url, options={}, &block) * field_set_tag(*args, &block) * error_messages_for(record, options={}) * label_tag(name, options={}, &block) * text_field_tag(name, options={}) * text_area_tag(name, options={}) * password_field_tag(name, options={}) * file_field_tag(name, options={}) * submit_tag(caption, options={}) ==== Format Helpers * relative_time_ago(date) * escape_javascript(html_content) === RenderPlugin This component provides a number of rendering helpers for sinatra, making the process of displaying templates far smoother. This plugin also has support for useful additions such as partials (with support for :collection) into the templating system. * erb_template(template_path, options={}) * haml_template(template_path, options={}) * render_template(template_path, options={}) * partial(template, *args) === WardenPlugin This component provides out-of-the-box support for Warden authentication. With this plugin registered, warden will be automatically required, configured and helpers will be provided to make interacting with warden dead simple. * current_user * authenticate_user! * logout_user! * logged_in? * authenticated?(&block) * must_be_authorized!(failure_path=nil) == Acknowledgements Thanks to keldredd for the sinatra-helpers code that helped me to create erb capture and concat methods! == Contributers * Nathan Esquenazi - Project creator and code maintainer * Arthur Chiu - Forming the idea and various code contributions == Note on Patches/Pull Requests * Fork the project. * Make your feature addition or bug fix. * Add tests for it. This is important so I don't break it in a future version unintentionally. * Commit, do not mess with rakefile, version, or history. * Send me a pull request. Bonus points for topic branches. == Copyright Copyright (c) 2009 Nathan Esquenazi. See LICENSE for details.