Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rework template naming to bring back globs.

- Added a new option, template_name_style, which can be used to control
  how a template name is generated. Defaults to the :path style.
- Allow glob patterns to be passed to the templates function again.
- The templates function now accepts a :relative_to option that can be
  used to define the root from which the :path-style template names are
  made relative to. Defaults to Sinatra's root path setting.
  • Loading branch information...
commit 05111676b1eba993e29b70960b7ac122b65012a3 1 parent 8d78bbc
Ben Scott authored
Showing with 109 additions and 28 deletions.
  1. +60 −5 README.md
  2. +49 −23 lib/sinatra/ember.rb
65 README.md
View
@@ -2,12 +2,14 @@
Helpers for serving an [Ember.js][ember] app from [Sinatra][sinatra].
-- Pre-compile handlebars templates from separate view files rather than littering them throughout your html.
+- Pre-compile handlebars templates from separate view files rather than
+ littering them throughout your html.
## Installation
-You can install Sinatra Ember as a Ruby gem. You can install it via `gem install`.
+You can install Sinatra Ember as a Ruby gem. You can install it via `gem
+install`.
``` console
$ gem install sinatra-ember
@@ -31,15 +33,68 @@ require 'sinatra/ember'
class App < Sinatra::Base
register Sinatra::Ember
- ember {
- templates '/js/templates.js', ['app/templates/*']
+ ember {
+ templates '/js/templates.js', ['app/templates/**/*.hbs'], :relative_to => 'app/templates'
+
+ # optional; defaults to :path
+ template_name_style :path
}
end
```
+
+## API reference
+
+### ember.templates
+
+Combines templates in the given paths into a single asset of those templates in
+javascript form.
+
+```ruby
+# Usage:
+ember {
+ templates 'URI', [PATH, PATH, ...], OPTIONS_HASH
+}
+```
+
+`URI`
+> a string defining where the templates will be served.
+
+`PATH`
+> a string or glob pattern describing a file or files that should be included
+
+#### Options
+
+`:relative_to`
+> a string describing a path relative to the Sinatra root from which the
+> template names should be made relative to when using the `:path` template
+> name style. For example, if `:relative_to` is `app/templates` and the `PATH`
+> `app/templates/views/post.hbs` will be named `views/post`.
+
+
+### ember.template\_name\_style
+
+Defines the style by which template names will be derived from the paths.
+
+```ruby
+# Usage:
+ember {
+ template_name_style :basename
+}
+```
+
+Valid options include:
+
+`:path`
+> the template will be named as a relative path to the template file
+`:basename`
+> the template will be named as the basename of the template file
+
+
## Sinatra AssetPack
-If you're using the [sinatra-assetpack][assetpack] gem, add your served templates to a package.
+If you're using the [sinatra-assetpack][assetpack] gem, add your served
+templates to a package.
[assetpack]: https://github.com/rstacruz/sinatra-assetpack
72 lib/sinatra/ember.rb
View
@@ -8,16 +8,6 @@ def self.registered(app)
app.extend ClassMethods
end
- def self.template_name(path, file)
- unless path.empty?
- file.gsub!(/^#{Regexp.quote(path)}\/?/, '')
- end
-
- file.slice!(/.(handlebars|hbs|hjs)/)
-
- file
- end
-
module ClassMethods
# set ember options
def ember(&block)
@@ -27,26 +17,38 @@ def ember(&block)
end
def ember_init!
- ember.template_packages.each do |route, globs|
+ ember.template_packages.each do |route, spec|
get route do
mtime, output = @template_cache.fetch(route) do
# find all the template files
- paths = globs.map do |glob|
- files = Dir[glob+"/**/*.{handlebars,hbs,hjs}"].map { |x| x.squeeze('/') }
- [glob, files.flatten]
+ paths = spec.globs.map do |glob|
+ glob = File.expand_path(File.join(settings.root, glob))
+ Dir[glob].map { |x| x.squeeze('/') }
end
- # build up template hash
- #template_paths = {}
- #paths.each do |path|
- # template_paths[File.basename(path, '.hbs')] = path
- #end
+ # define the portion of the path to remove
+ remove = settings.root
+ if spec.opts[:relative_to].empty?
+ remove = settings.root
+ else
+ remove = File.join(remove, spec.opts[:relative_to])
+ end
+ remove = File.expand_path(remove)
# build up the javascript
- templates = paths.map do |path, files|
+ templates = paths.map do |files|
files.map do |file|
+ # derive the template name
+ tmpl_name = file.sub(/\.(handlebars|hbs|hjs)$/, '')
+ if settings.ember.template_name_style == :path
+ tmpl_name.sub!(/^#{Regexp.quote(remove)}\//, '')
+ else
+ tmpl_name = File.basename(tmpl_name)
+ end
+
+ # add the template content
content = File.read(file)
- "Ember.TEMPLATES[#{Sinatra::Ember.template_name(path,file).inspect}] = Ember.Handlebars.compile(#{content.inspect});"
+ "Ember.TEMPLATES[#{tmpl_name.inspect}] = Ember.Handlebars.compile(#{content.inspect});"
end
end
@@ -81,13 +83,37 @@ class Options
def initialize(app, &block)
@app = app
+ @template_name_style = :path
@template_packages = {}
instance_eval(&block)
end
- def templates(route, files=[])
- @template_packages[route] = files
+ def templates(route, files=[], opts={})
+ opts_defaults = {
+ :relative_to => '',
+ }
+ opts = opts_defaults.merge(opts)
+ @template_packages[route] = TemplateSpec.new(route, files, opts)
+ end
+
+ def template_name_style(*args)
+ val = args.first
+ if not val.nil?
+ @template_name_style = val
+ end
+ return @template_name_style
+ end
+ alias :template_name_style= :template_name_style
+ end
+
+ class TemplateSpec
+ attr_accessor :route, :globs, :opts
+
+ def initialize(route, globs, opts)
+ @route = route
+ @globs = globs
+ @opts = opts
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.