Skip to content

kontera-technologies/dust-rails

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Now following LinkedIn managed version as original repository is not being updated anymore.

Dust-Rails

This gem adds the Dust template and a corresponding assets engine to the asset pipeline in Rails => 3.1 applications.

For detailed information about Dust, visit http://github.com/linkedin/dustjs/

Installing

Add the following line to your Gemfile:

gem 'dust-rails'

Update your bundle:

bundle install

Usage

Place individual Dust template file in their own file with template_name.js.dust extension.

	/* app/assets/javascripts/templates/demo.js.dust */
	
	Hello {name}! You have {count} new messages.

Which will be compiled and rendered as:

	(function(){dust.register("demo",body_0);function body_0(chk,ctx){return chk.write("Hello ").reference(ctx.get("name"),ctx,"h").write("! You have ").reference(ctx.get("count"),ctx,"h").write(" new messages.");}return body_0;})();

Dust-rails resolves the name of the template out of relative path of each template file. Relative path starts from app/assets/javascripts/templates/ by default.

app/assets/javascripts/templates/demo1.js.dust -> demo1
app/assets/javascripts/templates/demos/demo2.js.dust -> demos/demo2

If you want to change the default root path of template files, add following configuration into application.rb:

    # config/application.rb
    module YourApp
        class Application < Rails::Application
            config.dust.template_root = 'app/assets/your_path_to_templates/'
        end
    end

If you don't want to automatically load the dust template, add the following configuration into application.rb:

    # config/application.rb
    module YourApp
        class Application < Rails::Application
            config.dust.autoload = false
        end
    end

If you want to use AMD style templates, add the following configuration into application.rb:

    # config/application.rb
    module YourApp
        class Application < Rails::Application
            config.dust.use_amd = true
        end
    end

and then you can depend on the template in AMD by using:

  define(['dust!demos/demo2'], function() {
  	dust.render("demos/demo2", {name: "Fred", count: 10}, function(err, out) {
 	  	console.log(out);
  	});
  });

In your javascript files, require dust-core and your own template files. Using require_tree is recommended if you want to require all the template files at once.

	/* app/assets/javascripts/application.js */
	
	//= require dust-core
	//= require dust-helpers
	//= require dust-filters-secure
	//= require_tree ./templates
	...
	dust.render("demo", {name: "Fred", count: 10}, function(err, out) {
 		console.log(out);
	});

All done. Your template files will be compiled and registered.

About

__DEPRECATED__Use dust.js with rails

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%