Git Product home page Git Product logo

handlebars-rails's Introduction

What

Use the awesome Handlebars.js both server- and client-side.

Why

DRY. That's why.

Getting Handlebars

The easiest way to get a current distribution of handlebars is to download it from https://github.com/wycats/handlebars.js/downloads.

How

First, grab the copy of handlebars you are using and copy it into vendor/javascripts/handlebars.js. If you want, you can symlink it from your public directory.

In app/controllers/blogs_controller.rb:

BlogsController
  def show
    @blog = Blog.find(params[:id])
  end
end

In app/views/blogs/show.html.hbs:

<article>
  <header>
    <h1>{{{ blog/title }}}</h1>
  </header>
  {{{ blog/body }}}
  <footer>
    by {{{ link_to blog/author/name blog/author }}}
</article>

Usage Gotchas

  • Template line numbers may not match stack trace line numbers. This will be resolved upstream.
  • Block helpers do not currently work.

HTML-Safety

Rails returns HTML-safe strings, but the string-safety information isn't passed into Handlebars, so Handlebars re-escapes the content. To get around this, use the triple-stash ({{{ ... }}}) when using a Rails helper. See issue 2.

Rails Helpers

Rails helpers obviously do not exist in the client-side JS context. This means that if you use {{{ link_to ... }}}, it can only be run server-side. The solution is to implement a minimal link_to in the client-side. See issue 4 and issue 5.

Credits

Yehuda Katz did all the heavy lifting to get this off the ground, both in terms of writing Handlebars.js and the template handler here. Additional huge props to Charles Lowell for therubyracer, a sine qua non for this project.

handlebars-rails's People

Contributors

andrewjanssen avatar cowboyd avatar wycats avatar

Watchers

 avatar  avatar

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.