Matt Darby
Matt Darby

Reputation: 6324

AngularJS and Rails template engines

As a Rails guy, I love me some Haml or Slim. However, I haven't had much luck with either when it comes to using AngularJS. Is there a templating engine that is AngularJS friendly and is less verbose than straight HTML?

Upvotes: 4

Views: 1234

Answers (2)

jacklin
jacklin

Reputation: 2779

I'm using slim for my angularjs templates. It took me forever to figure out how to do it, but its very easy to do once you know how.

Add the slim gem and bundle install.

Make sure you have a template directory. Mine is:

assets/javascripts/templates

Then create a file in config/initializers. Mine is just slim_engine.rb. The name doesn't matter.

Then in that file include this line:

Rails.application.assets.register_engine('.slim', Slim::Template)

Here's a snippet of the route provider I have that uses one of these templates:

angular.module('customer_app', ['customer_service'])
  .config(['$routeProvider', function($provider) {
    $provider.when('/:customer_id/edit/', { templateUrl: '/assets/customers/edit.html.slim', controller: 'CustomersController' });
  }])
  .config(["$httpProvider", function(provider) {
    provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
  }]);

The full template path is: assets/templates/customers/edit.html.slim

Hope this helps!

Upvotes: 1

Khaja Minhajuddin
Khaja Minhajuddin

Reputation: 6751

I have written about how you can use haml for your angularjs templates with rails and properly load all the templates in one request here: http://minhajuddin.com/2013/04/28/angularjs-templates-and-rails-with-eager-loading

Upvotes: 0

Related Questions