Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add custom_escape option. closes #3

  • Loading branch information...
commit 424a54806e8637b9ace91deb25a421739dd07e1f 1 parent d932e79
@dnagir authored
View
15 README.md
@@ -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)
View
6 lib/ruby-haml-js/template.rb
@@ -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
7 spec/template_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.