Giter Club home page Giter Club logo

barber's Introduction

Barber Build Status

Barber handles all your Handlebars precompilation needs. You can use Barber as part of your project build system or with someting like rake-pipeline.

Installation

Add this line to your application's Gemfile:

gem 'barber'

And then execute:

$ bundle

Or install it yourself as:

$ gem install barber

Usage

You will mainly interact with the utility classes. The utitlity classes delegate to the actual precompiler. They support two primary use cases:

  1. Precompiling individual handlebars files.
  2. Precompiling inline handlebars templates.

Here are some code examples:

require 'barber'

Barber::FilePrecompiler.call(File.read("template.handlebars"))
# "Handlebars.template(function(...));"

Barber::InlinePreCompiler.call(File.read("template.handlebars"))
# Note the missing semicolon. You can use this with gsub to replace
# calls to inline templates
# "Handlebars.template(function(...))"

Barber also packages Ember precompilers.

require 'barber'

Barber::Ember::FilePrecompiler.call(File.read("template.handlebars"))
# "Ember.Handlebars.template(function(...));"

Barber::Ember::InlinePrecompiler.call(File.read("template.handlebars"))
# "Ember.Handlebars.template(function(...))"

Building Custom Precompilers

All of Barber's utility classes (demoed above) delegate to Barber::Precompiler. Barber::Precompiler implements a simple public interface you can use to to build your own. A precompiler simply exposes a Barber.precompile JS property. Override Barber::Precompiler#sources to setup your own. Source must respond to #read. Here is an example:

require 'barber'
require 'stringio'

class CustomPrecompiler < Barber::Precompiler
  def sources
    [StringIO.new(custom_compiler)]
  end

  def custom_compiler
    %Q[var Barber = { precompile: function(template) { return "Hello World!" } };]
  end
end

Usage with rake-pipeline-web-filters

All the utility classes implement call. This means they can be subsituted for procs/lambda where needed. Here's how you can get precompilation of your ember templates with rake-pipeline-web-filters:

require 'barber'

match "**/*.handlebars" do
  handlebars :wrapper_proc => Barber::Ember::FilePrecompiler
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

barber's People

Contributors

tchak avatar machty avatar ianpetzer avatar

Watchers

Ryunosuke Sato avatar James Cloos 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.