Use Mustache in your Sinatra app without the extra view classes
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ext
lib
spec
.gitignore
.rspec
.travis.yml
Changelog.md
Gemfile
License.txt
README.md
Rakefile
sinatra-mustache.gemspec

README.md

sinatra-mustache Build Status Code Climate Dependency Status

To simplify setting up Sinatra to use Mustache for it's templates. Supports MRI 1.9, 2.0 as well as Rubinius and JRuby in 1.9 and 2.0 mode.

Sinatra is a amazing little web framework, if you don't know much about it you should take some time to get to know it.

Mustache is a really great and simple templating system.

Without extra view classes

There is already an official way to get Mustache to work with Sinatra, which involves some work and requires separate view classes to accompany your mustache templates. There's power in that for sure, but for some implementations it is a bit complex.

Usage

Try this on:

require 'sinatra/mustache'

class App < Sinatra::Base
  set :views, 'templates' # totally optional
end

And then put your .mustache files in your app's views folder.

Instance variables and locals are available to the template as well as YAML front matter.

Helpers

Use the helpers you defined in your app by passing their names to mustache_helper:

require 'sinatra/mustache'

class App < Sinatra::Base
  register Sinatra::MustacheHelper #makes mustache_helper available
  
  helpers do
    def some_helper *args
      args.join('/')
    end
  end
  
  mustache_helper :some_helper
end

Now you can use some_helper in your mustache template. There is also support for mustache's dot-notation:

" {{ some_helper.awesome.nice.spectacular }}" #=> "awesome/nice/spectacular"

Also, There is support for registering helper modules

module SomeHelpers
  def hello what
    "Hello #{what}"
  end
end

class App < Sinatra::Base
  register Sinatra::MustacheHelper
  helpers SomeHelpers
  mustache_helpers SomeHelpers
end

Now, this is possible:

" {{ hello.Mars }}" #=> "Hello Mars"

Passing arguments to helper methods via dot notation is limited to Strings only.

Caveat

If you need the extra support of the ruby views used in the official Mustache for Sinatra example this gem probably isn't for you.

Contributing to sinatra-mustache

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Make sure to add documentation for it. This is important so everyone else can see what your code can do.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2013 Beat Richartz. See LICENSE.txt for further details.