Git Product home page Git Product logo

alphagov.shared_mustache's Introduction

SharedMustache

Add the ability to share mustache templates in Rails and JavaScript.

Mustache templates will be rendered using Ruby mustache on the server.

On the client there is a jQuery helper to let you use the template. In development the templates are outputted on the page in script blocks. For production the templates are compiled using hogan.js, the compiled templates can then be included into your standard JavaScript file using the asset-pipeline.

Installation

Add 'shared_mustache' to your your application's Gemfile.

By default when you run rake shared_mustache:compile it will put the outputed templates JavaScript at Rails.root/app/assets/javascript/templates.js. This can be configured using an intializer. The templates.js file should be kept empty for development and only populated in production.

You should add the following lines to your application.js:

//= require shared_mustache
//= require templates

This adds the jQuery helper for you to use the templates and in production will add the compiled templates.

At the bottom of your main layout you should add:

<%= render_mustache_templates if Rails.env.development? %>

Add rake shared_mustache:compile before the assets:precompile step of your deploy script.

Currently it expects your views to be found under app/views.

Usage

Create mustache templates as you would erb partials. So for example at app/views/home/_row.mustache. You can then render it in an erb template using:

<%= render_mustache('home/row', context) %>

It tries to use the same conventions as the native render methods. Such that you can ommit the controller if the mustache is in the same folder as the currently executing controller.

In your JavaScript you should get access to jQuery methods to interact with the mustache templates:

$.mustache('home/_row', context);

alphagov.shared_mustache's People

Contributors

edds avatar h-lame avatar fofr avatar evilstreak avatar bradwright avatar tekin avatar alexmuller avatar surminus avatar mattbostock avatar rboulton avatar tijmenb 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.