Giter VIP home page Giter VIP logo

mustache-sinatra's Introduction

Mustache::Sinatra

Build Status

Support for Mustache in your Sinatra app.

Installation

Add this line to your application's Gemfile:

gem 'mustache-sinatra'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mustache-sinatra

Usage

require 'mustache/sinatra'

class Hurl < Sinatra::Base
  register Mustache::Sinatra

  set :mustache, {
    # Should be the path to your .mustache template files.
    :templates => "path/to/mustache/templates",

    # Should be the path to your .rb Mustache view files.
    :views => "path/to/mustache/views",

    # This tells Mustache where to look for the Views module,
    # under which your View classes should live. By default it's
    # the class of your app - in this case `Hurl`. That is, for an :index
    # view Mustache will expect Hurl::Views::Index by default.
    # If our Sinatra::Base subclass was instead Hurl::App,
    # we'd want to do `set :namespace, Hurl::App`
    :namespace => Hurl
  }

  get '/stats' do
    mustache :stats
  end
end

As noted above, Mustache will look for Hurl::Views::Index when mustache :index is called.

If no Views::Stats class exists Mustache will render the template file directly.

You can indeed use layouts with this library. Where you'd normally <%= yield %> you instead {{{yield}}} - the body of the subview is set to the yield variable and made available to you.

If you don't want the Sinatra extension to look up your view class, maybe because you've already loaded it or you're pulling it in from a gem, you can hand the mustache helper a Mustache subclass directly:

# Assuming `class Omnigollum::Login < Mustache`
get '/login' do
  @title = "Log In"
  require 'lib/omnigollum/views/login'
  mustache Omnigollum::Login
end

Contributing

  1. Fork it ( https://github.com/[my-github-username]/mustache-sinatra/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

mustache-sinatra's People

Contributors

locks avatar thijswouters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mustache-sinatra's Issues

NoMethodError view_namespace=

I'm unable to start the example code.

NoMethodError at /
undefined method `view_namespace=' for #<Class:0x007f791c509c80>
file: sinatra.rb location: block in compile_mustache line: 95

I found out that sinatra needs an update for Rack 1.6 that still hasn't made it into a release, so I pinned Rack at '< 1.6'

I have a working example app from Mustache 0.99.4 with some Gemfile resources locked, not sure what else might have changed, but I am pretty sure I shouldn't need to require all of these view classes by hand or do any weird hacks like $:<<'./'

I posted at kingdonb/mustache-sinatra-example#07532cdd my working example from that older version.

The next commit shows a fresh Gemfile.lock after deleting it and running bundle update, with the new gem 'mustache-sinatra', I get the undefined method view_namespace=. I don't know why this doesn't work. Tried with several versions of ruby. Maybe I'm using it wrong.

Can't render literal templates

Hi, I can't seem to manage to render a template from a string variable, it just tries to find a file with that name.

mustache "Hello World"

gives an error page:

Errno::ENOENT at /page
No such file or directory @ rb_sysopen - path/to/sinatra/views/Hello World.mustache
file: settings.rb location: read line: 142

whereas

erb "Hello World"

works as expected and prints "Hello World" to screen

Apologies if I'm missing something obvious but I've searched at length and haven't managed to find anything that could help me.

Depend on newer mustache for Ruby 3.2 compatibilty

Until now mustache-sinatra has been working fine despite depending on an older version of mustache (<= 1.00). However, that version of mustache is sadly no longer compatible with Ruby v3.2. Is anyone still maintaining this library, and how much work would it be to cut a new gem release depending on a newer mustache version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.