Skip to content

Instantly share code, notes, and snippets.

@bcardiff
Created July 17, 2013 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bcardiff/6021459 to your computer and use it in GitHub Desktop.
Save bcardiff/6021459 to your computer and use it in GitHub Desktop.
Render views as angular text/ng-template in rails

Usage

1) Drop views and partials anywhere in app/views

|-- app
|   `-- views
|       `-- shared
|           `-- angular_views
|               |-- foo
|               |   `-- view3.html.haml
|               |-- _partial1.html.haml
|               |-- view1.html.haml
|               `-- view2.html.haml

2) Specify which directory should be included

%div(ng-app='MyApp')
  - angular_templates 'shared/angular_views'

3) Use the following url as templateUrl

  • view1.html

  • view2.html

  • foo/view3.html

N.B.: Partials are not exposed

module AngularHelper
def angular_templates(location)
app_views_location = File.join(Rails.root, 'app', 'views')
base_location = File.join(app_views_location, location)
to_strip_for_client_path = base_location
to_strip_for_view_path = app_views_location
Dir[File.join(base_location, '**', '*.html.haml')].each do |f|
next if File.basename(f).start_with? '_'
client_path = f[to_strip_for_client_path.length+1..-1].sub(/\.html\.haml$/, '.html')
view_path = f[to_strip_for_view_path.length+1..-1].sub(/\.html\.haml$/, '')
haml_tag :script, type: 'text/ng-template', id: client_path do
haml_concat render file: view_path
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment