Giter VIP home page Giter VIP logo

ruby-haml-js's Introduction

Ruby HAML-JS

Precompiles the HAML-JS templates into a function.

This can be used as a standalone template or as part of Rails 3.1 assets pipeline.

For example, if you have a file app/assets/javascripts/templates/comment.jst.hamljs with the following content:

.comment
  .author= author
  .text= text

Then it will be compiled on the server side into a plain JavaScript function. It doesn't require you to do anything on the client side. And the result will look like this:

(function() {
  this.JST || (this.JST = {});
  this.JST["templates/comment"] = function (locals) { function html_escape(text) {
      return (text + "").
        replace(/&/g, "&").
        replace(/</g, "&lt;").
        replace(/>/g, "&gt;").
        replace(/\"/g, "&quot;");
    }
  with(locals || {}) {
    try {
     var _$output="<div class=\"comment\"><div class=\"author\">" + 
  html_escape(author) + 
  "</div><div class=\"text\">" + 
  html_escape(text) + 
  "</div></div>";
   return _$output;  } catch (e) {
      return "\n<pre class='error'>" + html_escape(e.stack) + "</pre>\n";
    }
  }
  };
}).call(this);

Then you access that template like so:

var commentView = JST['templates/comment'];
// ...
// Somewhere in your code later:
var html = commentView({author: 'Dima', text: 'Looks nice'});

Installation

# Gemfile
gem 'ruby-haml-js'
# Then install it
bundle

Usage - with Rails 3.1 assets pipeline

  1. Put your template unders app/assets/javascripts (or other path where Rails can find it).
  2. Use the naming my-template.jst.hamljs.
  3. Serve the template to browser by require my-template from application.js or link to it as my-template.js
  4. Use the template from the JavaScript: JST['my-template']({your: 'local', variables: 'go here'})

Configuration

Custom HTML escape

You can change the escape function from the built-in, autogenerated to your own:

# Set it somewhere, before generating the template.
# Good place can be a Rails initializer.
RubyHamlJs::Template.custom_escape = "App.escape_html"

This will use the given function and will not generate custom escape code inside each template. But you need to make sure that the function is defined before using the templates in JavaScript.

Custom haml.js

If you need a custom version of haml.js library you may specify it using the haml_path

RubyHamlJs::Template.haml_path = "path/to/haml.js"

Development

Pull requests are very welcome! But please write the specs.

To start, clone the repo and then:

bundle install
bundle exec rspec spec/

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.