diff --git a/README.md b/README.md index 7842bc3..a9a9f18 100644 --- a/README.md +++ b/README.md @@ -163,11 +163,8 @@ config.hamlcoffee.format = 'xhtml' ### Template namespace -By default all Haml Coffee templates are registered under the `JST` namespace. - -**Example:** - -A template `app/assets/templates/header.hamlc` with the given content: +By default all Haml Coffee templates are registered under the `JST` namespace. A template +`app/assets/templates/header.hamlc` with the given content: ```haml %header @@ -208,7 +205,7 @@ And you must make sure `MyApp` exists before any template is loaded. #### Template name The name under which the template can be addressed in the namespace depends not only from the filename, but also on -the directory name. +the directory name by default. The following examples assumes a configured namespace `window.JST` and the asset template directory `app/assets/templates`: @@ -217,6 +214,25 @@ The following examples assumes a configured namespace `window.JST` and the asset * `app/assets/templates/users/new.hamlc` will become `JST['users/new']` * `app/assets/templates/shared/form/address.hamlc` will become `JST['shared/form/address']` +### Basename + +If you don't want to have your directory names under which your template is located to be contained in the JST name, +you can configure Haml Coffee n your `config/application.rb` to strip off the path to the file name and only use the +basename as JST name: + +```ruby +config.hamlcoffee.basename = true +``` + +With this setting enabled the following naming rule applies: + +* `app/assets/templates/login.hamlc` will become `JST['login']` +* `app/assets/templates/users/new.hamlc` will become `JST['new']` +* `app/assets/templates/shared/form/address.hamlc` will become `JST['address']` + +This setting has only an effect when you're using Haml Coffee to generate the JST and not when using the Sprockets +JST processor. + ### Escaping All generated output by running CoffeeScript in your template is being escaped, but you can disable escaping of either diff --git a/lib/haml_coffee_assets/engine.rb b/lib/haml_coffee_assets/engine.rb index 3c80471..5912a31 100644 --- a/lib/haml_coffee_assets/engine.rb +++ b/lib/haml_coffee_assets/engine.rb @@ -12,6 +12,7 @@ class Engine < Rails::Engine :format => 'html5', :namespace => 'window.JST', :uglify => false, + :basename => false, :escapeHtml => true, :escapeAttributes => true, :cleanValue => true, @@ -39,6 +40,7 @@ class Engine < Rails::Engine config.namespace = options[:namespace] config.format = options[:format] config.uglify = options[:uglify] + config.basename = options[:basename] config.escapeHtml = options[:escapeHtml] config.escapeAttributes = options[:escapeAttributes] config.cleanValue = options[:cleanValue] diff --git a/lib/haml_coffee_assets/haml_coffee.rb b/lib/haml_coffee_assets/haml_coffee.rb index fb14320..8dc3ed6 100644 --- a/lib/haml_coffee_assets/haml_coffee.rb +++ b/lib/haml_coffee_assets/haml_coffee.rb @@ -21,6 +21,11 @@ module HamlCoffee mattr_accessor :uglify self.uglify = false + # Ignore path when generate the JST + # + mattr_accessor :basename + self.basename = false + # Escape template code output # mattr_accessor :escapeHtml @@ -87,7 +92,7 @@ def configure # @return [String] the compiled template in JavaScript # def compile(name, source, jst = true) - runtime.call('HamlCoffeeAssets.compile', name, source, jst, HamlCoffee.namespace, HamlCoffee.format, HamlCoffee.uglify, + runtime.call('HamlCoffeeAssets.compile', name, source, jst, HamlCoffee.namespace, HamlCoffee.format, HamlCoffee.uglify, HamlCoffee.basename, HamlCoffee.escapeHtml, HamlCoffee.escapeAttributes, HamlCoffee.cleanValue, HamlCoffee.customHtmlEscape, HamlCoffee.customCleanValue, HamlCoffee.customPreserve, HamlCoffee.customFindAndPreserve, diff --git a/lib/haml_coffee_assets/haml_coffee_assets.js b/lib/haml_coffee_assets/haml_coffee_assets.js index 52d81b7..704c8e9 100644 --- a/lib/haml_coffee_assets/haml_coffee_assets.js +++ b/lib/haml_coffee_assets/haml_coffee_assets.js @@ -25,6 +25,7 @@ var HamlCoffeeAssets = (function(){ * @param jst [Boolean] if a JST template should be generated * @param format [String] output HTML format * @param uglify [Boolean] skip HTML indention + * @param basename [Boolean] ignore path when generate JST * @param escapeHtml [Boolean] whether to escape HTML output by default or not * @param escapeAttributes [Boolean] whether to escape HTML attributes output by default or not * @param customHtmlEscape [String] the name of the function to escape the output @@ -33,7 +34,7 @@ var HamlCoffeeAssets = (function(){ * @param selfCloseTags [String] comma separated list of tags to self-closing tags * @param context [String] the name of the function to merge contexts */ - compile: function(name, source, jst, namespace, format, uglify, + compile: function(name, source, jst, namespace, format, uglify, basename, escapeHtml, escapeAttributes, cleanValue, customHtmlEscape, customCleanValue, customPreserve, customFindAndPreserve, preserveTags, selfCloseTags, @@ -42,6 +43,7 @@ var HamlCoffeeAssets = (function(){ var compiler = new Compiler({ format: format, uglify: uglify, + basename: basename, escapeHtml: escapeHtml, escapeAttributes: escapeAttributes, cleanValue: cleanValue, diff --git a/lib/js/haml-coffee.js b/lib/js/haml-coffee.js index 0eac072..68be61b 100644 --- a/lib/js/haml-coffee.js +++ b/lib/js/haml-coffee.js @@ -342,7 +342,7 @@ require.define("/haml-coffee.js", function (require, module, exports, __dirname, module.exports = HamlCoffee = (function() { function HamlCoffee(options) { - var _base, _base2, _base3, _base4, _base5, _base6, _base7, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; + var _base, _base2, _base3, _base4, _base5, _base6, _base7, _base8, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8; this.options = options != null ? options : {}; if ((_ref = (_base = this.options).escapeHtml) == null) { _base.escapeHtml = true; @@ -354,14 +354,17 @@ require.define("/haml-coffee.js", function (require, module, exports, __dirname, _base3.cleanValue = true; } if ((_ref4 = (_base4 = this.options).uglify) == null) _base4.uglify = false; - if ((_ref5 = (_base5 = this.options).format) == null) { - _base5.format = 'html5'; + if ((_ref5 = (_base5 = this.options).basename) == null) { + _base5.basename = false; } - if ((_ref6 = (_base6 = this.options).preserveTags) == null) { - _base6.preserveTags = 'pre,textarea'; + if ((_ref6 = (_base6 = this.options).format) == null) { + _base6.format = 'html5'; } - if ((_ref7 = (_base7 = this.options).selfCloseTags) == null) { - _base7.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base'; + if ((_ref7 = (_base7 = this.options).preserveTags) == null) { + _base7.preserveTags = 'pre,textarea'; + } + if ((_ref8 = (_base8 = this.options).selfCloseTags) == null) { + _base8.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base'; } } @@ -535,7 +538,7 @@ require.define("/haml-coffee.js", function (require, module, exports, __dirname, if (namespace == null) namespace = 'window.HAML'; template = ''; segments = ("" + namespace + "." + templateName).replace(/(\s|-)+/g, '_').split(/\./); - templateName = this.options.basename ? segments.pop() : segments.pop().split(/\/|\\/).pop(); + templateName = this.options.basename ? segments.pop().split(/\/|\\/).pop() : segments.pop(); namespace = segments.shift(); if (segments.length !== 0) { for (_i = 0, _len = segments.length; _i < _len; _i++) { diff --git a/spec/haml_coffee_assets/haml_coffee_spec.rb b/spec/haml_coffee_assets/haml_coffee_spec.rb index add1e9a..1abd2aa 100644 --- a/spec/haml_coffee_assets/haml_coffee_spec.rb +++ b/spec/haml_coffee_assets/haml_coffee_spec.rb @@ -7,6 +7,7 @@ HamlCoffeeAssets::HamlCoffee.namespace = 'window.JST' HamlCoffeeAssets::HamlCoffee.format = 'html5' HamlCoffeeAssets::HamlCoffee.uglify = false + HamlCoffeeAssets::HamlCoffee.basename = false HamlCoffeeAssets::HamlCoffee.preserveTags = 'textarea,pre' HamlCoffeeAssets::HamlCoffee.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base' HamlCoffeeAssets::HamlCoffee.escapeHtml = true @@ -447,6 +448,47 @@ end end + context 'basename configuration' do + it 'does not strip the path by default' do + subject.compile('path/to/file', "%p Basename").should eql <<-TEMPLATE +(function() { + var _ref; + if ((_ref = window.JST) == null) { + window.JST = {}; + } + window.JST['path/to/file'] = function(context) { + return (function() { + var $o; + $o = []; + $o.push("

Basename

"); + return $o.join("\\n"); + }).call(context); + }; +}).call(this); + TEMPLATE + end + + it 'does strip the path' do + subject.basename = true + subject.compile('path/to/file', "%p Basename").should eql <<-TEMPLATE +(function() { + var _ref; + if ((_ref = window.JST) == null) { + window.JST = {}; + } + window.JST['file'] = function(context) { + return (function() { + var $o; + $o = []; + $o.push("

Basename

"); + return $o.join("\\n"); + }).call(context); + }; +}).call(this); + TEMPLATE + end + end + context 'whitespace tag list configuration' do it 'uses textarea and pre by default' do subject.compile('ws', "%textarea= 'Test\\nMe'\n%pre= 'Test\\nMe'\n%p= 'Test\\nMe'\n").should eql <<-TEMPLATE