shaven
文件大小: unknow
源码售价: 5 个金币 积分规则     积分充值
资源说明:(proof-of-concept) Templating without mustaches!
# Shaven - Templating without mustaches!

Hey guys, look at present fasion... mustaches are not fashionable anymore =P.
Take a look how nice looking are shaven templates.

## Motivation

I'm not a designer, usualy all templates in my work are prepared by external
design studios or freelancers... But of course they are always pure xhtml.
So still we have to deal with them, convert to haml, fill in with mustaches or
erb sh**t! Now, my patience is over. Shaven will readmit some MVPC's fresh
air to your web apps and allow you to get rid of stupid logic from your views. 

## Installation

Installation with rubygems should go without quirks. Shaven depends on Nokogiri - if
you don't have it installed yet then i recommend you to check out its documentation
to avoid problems.

    $ gem install shaven

## How it works?

Shaven views are splited into two layers (similar to defunk's mustache) - Template
and Presenter. Templates are pure html files, Presenters are ruby classes
which provides data for templates. Depending on the data type provided by
presenter's methods you can freely and easily manipulate all contents within
your templates. Ok, lets finish talking and take a look at examples...

### Simple usage

    class SimplePresenter < Shaven::Presenter
      def title
      	"Hello world!"
      end

      def description
        "Yeah, hello beautiful code..."
      end
    end

    html = <<-HTML
    
    
    
      Example title!
    
    
      

Example title

Example description...

HTML SimplePresenter.feed(html).to_html This code produces following html: Hello World!

Hello World!

Yeah, hello beautiful code...

### DOM manipulation class ManipulationPresenter < Shaven::Presenter # If you add parameter to the presenter method, original node will # passed to it. Given element is an Nokogiri::XML::Node with some # extra helpers for content manipulation. def login_link(node) node.update!(:href => logout_path, :method => "delete") end # You can use extra html helpers to create new dom elements... def home_page_link a(:href => root_path, :class => "home-page-link") { "Go home!" } end # ... or to replace current. def title(node) node.replace! { tag(:h1, :id => "header") { "This is Sparta! "} } end end html = <<-HTML
Example title
Logout!
Home page link will go here...
HTML ManipulationPresenter.feed(html).to_html Result:

This is Sparta!

Logout!
Go Home!
### Hash scopes and lists Now, the true power of Shaven. Suport for lists and scopes. class ComplexPresenter < Shaven::Presenter # As scopes are treaded all hashes and objects responding to `#to_shaven` # method (which returns hash with attributes). def user { :name => "John Doe", :email => "john@doe.com", } end def users_list [ { :name => tag(:strong) { "Emmet Brown" }, :email => "emmet@brown.com"}, { :name => proc { |node| node.update!(:class => "marty") { "Marty Macfly" }, :email => "marty@macfly.com" }, { :name => "Biff Tannen", :email => "biff@tannen.com" } ] end end html = <<-HTML

Single user here!

Sample name

sapmle@email.com

More users

  • Sample name sample@email.com
HTML And the awesome result is:

Single user here!

Adam Smith

adam@smith.com

More users

  • Emmet Brown brown@brown.com
  • Marty Macfly marty@macfly.com
  • Biff Tannen biff@tannen.com
### Conditionals class ConditionalsPresenter < Shaven::Presenter def true? true end def false? false end end html = <<-HTML
Hello...
World!
HTML ### Dummy elements html = <<-HTML

Hello dummies!

This is dummy text!
HTML produces:

Hello dummies!

## Benchmarks Ruby 1.9.2 user system total real Shaven 3.480000 0.050000 3.530000 ( 3.539728) ERB 1.360000 0.010000 1.370000 ( 1.374822) Mustache 6.830000 0.080000 6.910000 ( 6.904736) HAML 10.800000 0.080000 10.880000 ( 10.886872) Ruby 1.8.7 user system total real Shaven 8.510000 0.120000 8.630000 ( 8.658850) ERB 1.510000 0.010000 1.520000 ( 1.516075) Mustache 5.230000 0.070000 5.300000 ( 5.314266) HAML 13.100000 0.230000 13.330000 ( 13.337038)

本源码包内暂不包含可直接显示的源代码文件,请下载源码包。