Git Product home page Git Product logo

poirot's Introduction

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

poirot's People

Contributors

carlosramireziii avatar clemenshelm avatar dcu avatar ismasan avatar olivernn avatar shawndrost avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

poirot's Issues

undefined method `view_renderer'

I am getting this error with rails 3.0.10:

undefined method `view_renderer' for #<#<Class:0x0000000b9d2fc8>:0x0000000b9ccc90>

in

vendor/gems/poirot/lib/poirot/view.rb:41:in `assign_variables!'

Poirot With Cells

I'm trying to use Cells [https://github.com/apotonick/cells] to create reusable view components. The view that I want to make reusable already exists, and it uses Poirot templates. The problem is template_include_tag which requires that all templates live inside of app\views whereas Cell keeps its view templates in app\cells.

A secondary problem is that, while cells are like controllers, they don't implement the controller_name method. This could be hacked around using an absolute path if Poirot had some way to either specify a rooted path (kinda yuck) or to search for templates in the current folder. Regarding this last suggestion I'm not sure if there is a way to know from a helper what file it's running from. If not, maybe there could be some way to denote that the file references is rooted at app instead of app/views. Perhaps like ~/path/to/mustache/template

Use erb or haml in stache templates

It would be wonderful if stache could handle template files like _posts.html.haml.mustache or _posts.html.erb.mustache.
Is is possibile? I can help if you provide me with the right directions!

Thank you for this wonderful gem!

segmentation fault when rendering a partial

I am rendering a haml partial inside a Poirot::View subclass, something like this

class IndexView
    def add_something
        render :partial  => "path/to/haml/partial"
    end
 end

the rails process crashes with SEG FAULT in the "render :partial" line

Accessing View Class Methods via poirot JS

Hopefully my last issue. Any help would be greatly appreciated.

I'm trying to access my helper methods in my optional view class. For example:

# View Class
module Users
  class UserListView < Poirot::View
    def foo
      "bar"
    end
  end
end
// JS call (coffeescript)
users = poirot.userList()

When loading the template with data, anything I define inside the JS obviously makes it into the view except for {{ foo }}.

Support partials

It'd be nice if I could use the {{> partial}} syntax to render partials. Right now it doesn't find the partial; perhaps it's not going through the Rails asset pipeline at all?

Problem with parameterize in cucumber/selenium

When I run cucumber/selenium the execution gets stuck when template_include_tag is called. After playing around with it, I realized that the parameterize call causes this error.

I came up with a patch for the whole method (because also the template file wouldn't get closed):

module Poirot
  module AssetHelper
    def template_include_tag(*sources)
      sources.collect do |source|
        template = File.read(resolve_partial_path(source))
        template_name = source.to_s.split('/').last.dasherize
        content_tag :script, template.html_safe, :type => "text/mustache", :id => "#{template_name}-template"
      end.join("\n").html_safe
    end
  end
end

This works perfectly for me and even the template_name method is obsolete. Is there a case where parameterize is really necessary?

New version of mustache.js

Is it possible to have the last version of mustache.js? As it is stated here:

"NOTICE: The dot notation feature was recently implemented for the 0.4 release, which is not out as of Nov 9 2011. You can find the feature in the current master branch of mustachejs".

This new version brings the dot notation feature!

Poirot::View does not support Rails' render

Trying to use Rails' render method inside a Poirot::View when trying to render a rails partial call's Mustache's render (which is fine, but should be called out clearly in documentation).

The solution is to use controller.render_to_body if you want to render a partial into a mustache template. Again, just a doc update.

Best,
@jasim and @kaiwren

Work with ActionMailer

Hey, is there a way to get this to work for ActionMailer as well? Currently when I try to use a foo.text.mustache template, I get an exception in my Mailer class:

ActionView::Template::Error: undefined method `params' for #<MyMailer:0x007fa738e84c20>
    path/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.11/lib/action_view/helpers/controller_helper.rb:10:in `params'
    from path/.rvm/gems/ruby-1.9.3-p194/gems/poirot-0.2.4/lib/poirot/view.rb:8:in `initialize'

Poirot::View method_missing glitch

Hello,

I'm working on a copy of poirot that I have adapted for my needs, in the context of a 3.1 mountable engine, so I'm not sure if the problem exists on your version, but just in case you hit it :

I had to change the method_missing method in Poirot::View : https://github.com/olivernn/poirot/blob/master/lib/poirot/view.rb#L18

When I was calling "new_whatever_path" in a subclass, it returned me nil. Problem is, instance_var was tested to see if it was defined, which I don't understand how it can not be, since it's defined in the previous line.

So, I changed the method to :

def method_missing(method_name, *args, &block)
  if instance_variable_defined?( "@#{method_name}" ) && args.empty?
    instance_variable_get("@#{method_name}")
  else
    view_context.send(method_name,*args, &block)
  end
end

poirot not defined?

Thanks for the great gem. The only thing I'm having trouble with is accessing my templates client-side using poirot.viewName(), as it seems poirot isn't being defined in my js.

I'm using Rails 3.1, and the rails g poirot:installer isn't available. Any ideas as to what the issue might be?

Thanks again

Rails 4?

I'm finally upgrading an app to Rails 4 which relies heavily on Poirot. Upon server side render of a partial I'm getting:

undefined method `_partial_renderer' for #<ActionView::Renderer:0x007fde81364b08>

I assume this is because Rails 4 is not supported. Correct? What are my options?

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.