Git Product home page Git Product logo

moostachator's Introduction

Moostachator is a rails-3.1 engine thought to let you share your templates between server and client side.

Load a page, it's generated through server side, no wait for domready.

Want to go to an other page using js? Generate the page with mustache.js.

The javascript framework provided also handle pushtate routing (with hashtag routing fallback). So your users can benefit of fast javascript rendering (client side rendering), and fast reloading (server side rendering) without any need for you to duplicate dom generation.

Installation

1°) Add in your Gemfile :

gem 'moostachator', :git => 'git://github.com/oelmekki/moostachator.git'

2°) run "bundle install"

3°) Add in your config/routes.rb :

mount Moostachator::Engine => "/moostachator"

4°) Create directory app/templates/

5°) Add in your app/assets/javascripts/application.js :

//= require mootools
//= require mootools-more
//= require mootools_ujs
//= require moostachator/mustache
//= require moostachator/Framework/ClassMethods
//= require moostachator/Framework/Model
//= require moostachator/Framework/HashChange
//= require moostachator/Framework/Route
//= require moostachator/Framework/RouterModule
//= require moostachator/Framework/Template
//= require moostachator/Framework/View
//= require moostachator/Framework/Controller

You're done.

Usage

Create a page

Add your mustache template in app/templates/<controller_name>/<action_name>.html.mustache

Add your view class in app/views/<controller_name>/<action_name>.rb

Example from poirot :

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

Get templates from javascript

Load and cache all templates :

Framework.Template.requestAll( '/moostachator/templates' );

You can then access a template this way :

Framework.Template.request( '<controller_name>_<template_name>', function( template ){
  var template_string = template.render( data ); // bind object data to mustache template and render it as string
  var $template = template.toElement( data ); // bind object data to mustache template and return template as Element
});

If you doesn't want to load all templates, but only one when needed, you can rather pass an url :

Framework.Template.request( '<controller_name>_<template_name>', '/moostachator/templates/<controller_name>_<template_name>', function( template ){
  var template_string = template.render( data ); // bind object data to mustache template and render it as string
  var $template = template.toElement( data ); // bind object data to mustache template and return template as Element
});

Don't be afraid of repeating this command. If the template is already cached, it will be used without firing request.

i18n

Thanks to babilu, all your locales are available through I18n object. See the project page for more on its usage.

In mustache templates, you can use _i to access locales :

{{_i.articles.title}}

Is the same as erb :

<%=t 'articles.title' %>

javascript mvtc framework

moostachator includes my moo-nojs-mvc framework. See its documentation for more.

Aknowledgment

Moostachator is based upon some great libs :

  • mustache, the logic free templating engine

  • mustache.js, mustache javascript support

  • poirot, rails mustache support

  • babilu, make locales accessible through javascript

moostachator's People

Stargazers

 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.