Permalink
Browse files

Make template root path customizable

  • Loading branch information...
1 parent c893f01 commit 2280b9fe43e3576322653f11fb80a475c9ab3a83 @thefron committed Jan 3, 2012
Showing with 51 additions and 10 deletions.
  1. +21 −4 README.markdown
  2. +11 −2 lib/dust-rails.rb
  3. +3 −3 lib/dust-rails/rails/dust_template.rb
  4. +15 −0 lib/dust-rails/rails/railtie.rb
  5. +1 −1 lib/dust-rails/version.rb
View
25 README.markdown
@@ -16,10 +16,10 @@ Update your bundle:
## Usage
-Place individual Dust template file in their own file with `template_name.js.dust` extension:
+Place individual Dust template file in their own file with `template_name.js.dust` extension.
```javascript
- /* app/assets/javascripts/dusts/demo.js.dust */
+ /* app/assets/javascripts/templates/demo.js.dust */
Hello {name}! You have {count} new messages.
```
@@ -31,14 +31,31 @@ Which will be compiled and rendered as:
```
+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:
+
+```ruby
+ # config/application.rb
+ module YourApp
+ class Application < Rails::Application
+ config.dust.template_root = 'app/assets/your_path_to_templates/'
+ end
+ end
+```
+
In your javascript files, require `dust-core` and your own template files.
-I recommend you put all the template files under `assets/javascripts/dusts` and require using `require_tree` for easy use.
+Using `require_tree` is recommended if you want to require all the template files at once.
```javascript
/* app/assets/javascripts/application.js */
//= require dust-core
- //= require_tree ./dusts
+ //= require_tree ./templates
...
dust.render("demo", {name: "Fred", count: 10}, function(err, out) {
console.log(out);
View
13 lib/dust-rails.rb
@@ -1,9 +1,18 @@
require "dust-rails/version"
require "dust-rails/rails/engine.rb"
require "dust-rails/rails/dust_template.rb"
+require "dust-rails/rails/railtie.rb"
module Dust
- module Rails
- # Your code goes here...
+ class Config
+ attr_accessor :template_root
+ end
+
+ def self.config
+ @@config ||= Config.new
+ end
+
+ def self.configure
+ yield self.config
end
end
View
6 lib/dust-rails/rails/dust_template.rb
@@ -29,9 +29,9 @@ def prepare
end
def evaluate(scope, locals, &block)
- <<-TMPL
- #{Source.context.call("dust.compile", data, name)}
- TMPL
+ template_root = Dust.config.template_root
+ template_name = file.split(template_root).last.split('.',2).first
+ Source.context.call("dust.compile", data, template_name)
end
end
end
View
15 lib/dust-rails/rails/railtie.rb
@@ -0,0 +1,15 @@
+require 'rails'
+
+module Dust
+ module Rails
+ class Railtie < ::Rails::Railtie
+ config.dust = ActiveSupport::OrderedOptions.new
+
+ initializer "dust.configure" do |app|
+ Dust.configure do |config|
+ config.template_root = app.config.dust[:template_root] || 'app/assets/javascripts/templates/'
+ end
+ end
+ end
+ end
+end
View
2 lib/dust-rails/version.rb
@@ -1,5 +1,5 @@
module Dust
module Rails
- VERSION = "0.2.0"
+ VERSION = "0.3.0"
end
end

0 comments on commit 2280b9f

Please sign in to comment.