glenngillen / hamstache

Combine Mustache logic-less templates with Haml

Home Page:http://hamstache.herokuapp.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hamstache

Hamstache joins the crazy awesome power of the Haml templating language with Mustache to keep your views clean and free of logic.

Usage

Add Hamstache to your Gemfile:

gem "hamstache"

And run bundle install to ensure it's added to your apps dependencies. Now you can create your view using a mix of Haml and Mustache syntax. For example to serve a request to WelcomeController#index we'd have the following in app/views/welcome/index.html.hamstache:

%h1 Welcome!
%p Here comes a list:
%ul
  {{#items}}
    %li {{ name }}
  {{/ items}}

Next you need to define your Hamstache template, for this example the template is expected to be found at app/hamstaches/demo/index.rb and could look like the following:

class Welcome::Index < Hamstache::Template
  def items
    [{ :name => "Item 1"},
    { :name => "Item 2"},
    { :name => "Item 3"}]
  end
end

Now a request to WelcomeController#index would result in the following HTML output:

<h1>Welcome!</h1>
<p>Here comes a list:</p>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

For more details on what is possible, read up on the API docs for HAML and Mustache respectively.

Why would I want to use this?

Because keeping your views free of conditional logic makes things much easier to maintain, but I don't see why it should come at the expense of having to use ERB.

Compatibility

Only tested with Rails 3.x and MRI Ruby 1.9.2 so far.

Contributions

  • Glenn Gillen

Status

In production use and still under development.

Bugs, Feature Requests, etc.

If you think you've found a bug or have a feature you'd like to contribute here are the ways to let me know, in order of likelihood for a speedy resolution:

  • Fork the repo, add a test, implement the feature, send me a pull request
  • Fork the repo, add a failing test, send me a pull request
  • Create an issue on Github.

License

Hamstache is released under the MIT license.

Copyright (c) 2011 Glenn Gillen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Analytics

About

Combine Mustache logic-less templates with Haml

http://hamstache.herokuapp.com


Languages

Language:Ruby 100.0%