Permalink
Browse files

add custom_escape option. closes #3

  • Loading branch information...
1 parent d932e79 commit 424a54806e8637b9ace91deb25a421739dd07e1f @dnagir committed Nov 27, 2011
Showing with 26 additions and 2 deletions.
  1. +15 −0 README.md
  2. +4 −2 lib/ruby-haml-js/template.rb
  3. +7 −0 spec/template_spec.rb
View
@@ -70,6 +70,21 @@ bundle
4. Use the template from the JavaScript: `JST['my-template']({your: 'local', variables: 'go here'})`
+# Configuration
+
+## Custom HTML escape
+
+You can change the escape function from the built-in, autogenerated to your own:
+
+```ruby
+# Set it somewhere, before generating the template.
+# Good place can be a Rails initializer.
+RubyHamlJs::Template.custom_escape = "App.escape_html"
+```
+
+This will use the given function and will not generate custom escape code inside each template.
+But you need to make sure that the function is defined before using the templates in JavaScript.
+
# Development
- Source hosted at [GitHub](https://github.com/dnagir/ruby-haml-js)
@@ -44,16 +44,18 @@ def js_string str
end
def js_custom_escape
- # TODO: Figure out how to configure this
- escape_function = nil
+ escape_function = self.class.custom_escape
escape_function ? "'#{js_string escape_function}'" : 'null'
end
class << self
+ attr_accessor :custom_escape
+
def haml_source
# Haml source is an asset
@haml_source ||= IO.read File.expand_path('../../../vendor/assets/javascripts/haml.js', __FILE__)
end
+
end
end
View
@@ -18,6 +18,13 @@ def render haml, file = "file.jst.js.hamljs"
it { should include "function (locals) {" }
it { should include 'function html_escape' }
+
+ context "with custom HTML escape function" do
+ before { @original_escape = RubyHamlJs::Template.custom_escape }
+ before { RubyHamlJs::Template.custom_escape = "App.custom_escape" }
+ after { RubyHamlJs::Template.custom_escape = @original_escape }
+ it { should include "App.custom_escape(" }
+ end
end
describe 'executing' do

0 comments on commit 424a548

Please sign in to comment.