• Stars
    star
    108
  • Rank 309,689 (Top 7 %)
  • Language
    Ruby
  • Created about 13 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

mustaches in your rails

Poirot

Description

Allows you to use Mustache template partials in Rails, also ads a helper method to easily allow JavaScript to re-use the same templates.

Usage

Create a partial just like you would with erb, prefixing the name with an underscore.

app/views/posts/_post_list.html.mustache

The template will have access to all normal rails helper methods and any instance variables that were set in the controller. If you need more than this an optional view class can be included, it should have the same name as the partial, but without the underscore.

app/views/posts/post_list_view.rb

module Posts
  class PostListView < Poirot::View
    def foo
      "bar"
    end

    def post_link
      post_path(post)
    end
  end
end

The view class has access to all the normal Rails helpers and access to the controller instance variables, e.g @post becomes the method post.

Also included is a simple view helper for including mustache templates in a page ready for use by JavaScript.

<%= template_include_tag 'post_list' %>

The above will insert a script tag with the contents of the partial called post_list, the type will be set as text/mustache and the id will be post-list-template.

<script id="post-list-template" type="text/mustache">
  <!-- template will be here! -->
</script>

Javascript Helper

Poirot also adds a javascript helper for using mustache templates from the browser. In Rails 3.1 this will be automatically added to the asset pipeline, you just need to add //= require poirot in your application.js file.

If you are using rails 3 then you can run the rails g poirot:install to install the javascripts in your application.

Using the poirot javascript helper is simple, given a template added to the page using template_include_tag

<%= template_include_tag 'post_list' %>

You can render this template from javascript by doing the following

poirot.postList()

This will return the contents of the post_list template wrapped in a jQuery object, ready for inserting into the dom. If you have data to pass to the template then you can pass it as the argument to the function, e.g.

poirot.postList({foo: "bar"})

Using Handlebars.js

It is possible to use handlebars instead of mustache when rendering templates client side. To do this you need to first include the handlebars.js source. The instead of including //= require poirot in the asset pipeline you should include //= require poirot-handlebars

Using Hogan.js

It is also possible to use hogan.js instead of mustache when rendering templates client side. To do this you need to first include the hogan.js source. The instead of including //= require poirot in the asset pipeline you should include //= require poirot-hogan

Dependencies

  • Rails >3.0.0
  • Mustache

More

An example app using Poirot

Credits

Mark Evans & Oliver Nightingale

More Repositories

1

lunr.js

A bit like Solr, but much smaller and not as bright
JavaScript
8,711
star
2

davis.js

RESTful degradable JavaScript routing using pushState
JavaScript
532
star
3

augment.js

Modern JavaScript in your IE
JavaScript
429
star
4

supplement.js

JavaScript
61
star
5

dox-template

simple templates for dox
JavaScript
29
star
6

moonwalkers

Demo site for Lunr2
JavaScript
26
star
7

canvas.fm

An audio visualisation using the SoundCloud API, Mozilla Audio Data API & Canvas
JavaScript
23
star
8

rejex

JavaScript
21
star
9

rackjson

Ruby
15
star
10

notepad

Example app for using davis.js with mustache templates on both server and client
JavaScript
15
star
11

weather

A D3 visualisation of the UK temperature using data from the Met Office.
JavaScript
13
star
12

tlux

Luxurious tmux configuration
Ruby
11
star
13

lunr-index-builder

A simple command line too for building lunr indexes
JavaScript
7
star
14

flipper

jquery plugin for 3d flip animation
JavaScript
6
star
15

lunr-schema

Schema for serialised Lunr indexes
Ruby
5
star
16

lunr.ru.js

A Russian language extension to lunr.js
JavaScript
5
star
17

curry.js

JavaScript
5
star
18

emberjs-requirejs-example

Example using Ember.js 1.0 (pre) with Require.js / AMD.
JavaScript
4
star
19

dotfiles

my dotfiles
Shell
4
star
20

lunr.rs

Lunr backend implemented in Rust
Rust
4
star
21

jem

Gems with a J
Ruby
3
star
22

cdn

image resizing cdn
JavaScript
3
star
23

greyhound

Fast fuzzy filepath finder
Go
3
star
24

flatshare

Ruby
3
star
25

ff

Fast fuzzy file finder
Rust
3
star
26

lunr.fr.js

French language extension for lunr.js
JavaScript
3
star
27

ShiftSubtitle

Ruby
3
star
28

builder

javascript proxy experiment
JavaScript
2
star
29

led_board

A ruby wrapper for displaying messages on an LED display board
Ruby
2
star
30

json_tree

Print JSON structure
Ruby
2
star
31

ColourPicker

JavaScript
2
star
32

ghostwriter

A pre-push hook to prevent using the wrong email when pushing to a specific remote.
Rust
2
star
33

pi_wire

A ruby wrapper around wiringPi
Ruby
2
star
34

iportfolio

A Rails application for hosting portfolios online
JavaScript
2
star
35

Jam

JavaScript
2
star
36

bitsy

Simple Bitmask in Ruby
Ruby
2
star
37

downcount

Social event count down application
Ruby
2
star
38

hx-perf-spike

Ruby
1
star
39

PhoneBook

Ruby
1
star
40

MinimalPixel

JavaScript
1
star
41

rpb

Remote pbcopy & pbpaste
Go
1
star
42

mongorack

Ruby
1
star
43

f1_calendar

F1 Calendar
Ruby
1
star
44

rails_training

Ruby
1
star
45

cdn-test

sample rails app client for cdn
Ruby
1
star
46

goliath_mysql2_errors

This project demonstrates the mysql problems we are having with goliath
Ruby
1
star
47

cranfield

Parsers for the Cranfield Collection test corpus
Ruby
1
star
48

tracker

Simple time tracking tool for batman
Ruby
1
star
49

bump

A simple tool for managing project version files.
Rust
1
star
50

BBC

Simple ruby library to access the BBC Apis
Ruby
1
star
51

bamboo_gif

bamboo bot gif end point
Ruby
1
star
52

blockbuster

A Visualisation of Hollywood Blockbuster Films
JavaScript
1
star
53

rack_push

Ruby
1
star