opal / opal-haml

Opal + Haml = <3
MIT License
23 stars 5 forks source link

with Rails - Template['myTemplate'] => nilClass #2

Closed rohanthewiz closed 10 years ago

rohanthewiz commented 10 years ago

Could you give an example using the Rails Asset Pipeline? Where do we put the haml template, and what is the exact file extension to use? I've been trying many different combination with no success.

What exactly is the Template constant? What methods can we use for introspection?

I am using Rails 4 and Ruby 1.9.3 Everything else works like a charm! Thanks for your work on this phenomenal tool!

adambeynon commented 10 years ago

Hi. The Template constant is provided by https://github.com/opal/opal/blob/master/stdlib/template.rb. If you use puts Template.paths, it should print a list of all registered templates. The templates must be located somewhere inside the asset pipeline. I tend to put all myn inside a templates directory.

So, a simple haml template app/assets/javascripts/templates/my_template.haml:

%div
  Bore Da.

You can then use the usual sprockets directives inside application.js:

//= require templates/my_template
//= require_tree ./templates

Note Any template inside a templates directory will have the templates/ part of its name removed. e.g., the following templates:

app/assets/javascripts/foo.haml
app/assets/javascripts/templates/bar.haml

Can be accessed in opal as:

Template["foo"]  # => #<Template: @name="foo">
Template["bar"]  # => #<Template: @name="bar">
rohanthewiz commented 10 years ago

Awesome!! It works! So this info should be moved to the readme, right? I am not an expert, but would like to help with this project... documentation whatever. I noticed Opal.Array was missing a rotate function. Adapted one from the Rubinius project:

#app/assets/javascripts/core-ext/array.rb
class Array
    def rotate(n = 1)
        return [] if empty?
        ary = self.dup
        return ary if n <= 0
        idx = n % ary.size #limit to valid index
        ary[idx..-1] + ary[0..idx-1]
    end
end

Please improve and drop in for me. THANKS A MILLION for what you have done here. I believe that Opal will become the standard tool for high productivity JavaScript development!