4

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?

1
  • What issues did you come across? I haven't used a templating engine extensively with AngularJS, but I know it's possible. Commented Apr 19, 2013 at 17:02

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

this removes the best part of the asset pipeline, fwiw. You need to do asset_path("edit.html") (I believe) in order for production fingerprinting to work.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.