diff --git a/REFERENCE b/REFERENCE index ab6ad038..de9e5cfe 100644 --- a/REFERENCE +++ b/REFERENCE @@ -514,7 +514,26 @@ may be compiled to: Home + +=== Setting Options +Options can be set by setting the hash Haml::Template.options +from environment.rb. Available options are: + +[:suppress_eval] Whether or not attribute hashes and Ruby scripts + designated by = or ~ should be + evaluated. If this is true, said scripts are + rendered as empty strings. Defaults to false. + +[:precompiled] A string containing a precompiled Haml template. + If this is passed, template is ignored + and no precompilation is done. + +[:attr_wrapper] The character that should wrap element attributes. + This defaults to ' (an apostrophe). Characters + of this type within the attributes will be escaped + (e.g. by replacing them with ') if + the character is an apostrophe or a quotation mark. --- Copyright (c) 2006 Hampton Catlin diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 77634124..4441fecc 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -85,24 +85,13 @@ module Haml MID_BLOCK_KEYWORDS = ['else', 'elsif', 'rescue', 'ensure', 'when'] # Creates a new instace of Haml::Engine to compile the given - # template string. - # - # Available options are: - # - # [:suppress_eval] Whether or not attribute hashes and Ruby scripts - # designated by = or ~ should be - # evaluated. If this is true, said scripts are - # rendered as empty strings. Defaults to false. + # template string. See REFERENCE for available options. # - # [:precompiled] A string containing a precompiled Haml template. - # If this is passed, template is ignored - # and no precompilation is done. + #-- + # When adding options, remember to add information about them + # to REFERENCE! + #++ # - # [:attr_wrapper] The character that should wrap element attributes. - # This defaults to ' (an apostrophe). Characters - # of this type within the attributes will be escaped - # (e.g. by replacing them with ') if - # the character is an apostrophe or a quotation mark. def initialize(template, options = {}) @options = { diff --git a/lib/haml/template.rb b/lib/haml/template.rb index 4834a4b5..52e334bc 100644 --- a/lib/haml/template.rb +++ b/lib/haml/template.rb @@ -4,6 +4,20 @@ require 'action_view' module Haml class Template + + class << self + @@options = {} + + # Gets various options for HAML. See REFERENCE for details. + def options + @@options + end + + # Sets various options for HAML. See REFERENCE for details. + def options=(value) + @@options = value + end + end def initialize(view) @view = view @@ -29,11 +43,12 @@ module Haml self.class.send(:define_method, key) { val } end end - + if @precompiled = get_precompiled(template_file_name) - engine = Haml::Engine.new("", :precompiled => @precompiled) + options = { :precompiled => @precompiled }.merge @@options + engine = Haml::Engine.new("", options) else - engine = Haml::Engine.new(File.read(template_file_name)) + engine = Haml::Engine.new(File.read(template_file_name), @@options) set_precompiled(template_file_name, engine.precompiled) end diff --git a/test/results/eval_suppressed.xhtml b/test/results/eval_suppressed.xhtml new file mode 100644 index 00000000..c79ba72b --- /dev/null +++ b/test/results/eval_suppressed.xhtml @@ -0,0 +1,2 @@ +

+

Me!

diff --git a/test/template_test.rb b/test/template_test.rb index 4ae8e93c..f392cb01 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -77,6 +77,13 @@ class TemplateTest < Test::Unit::TestCase assert !(res.nil? || res.empty?) end + def test_haml_options + Haml::Template.options = { :suppress_eval => true } + assert_equal({ :suppress_eval => true }, Haml::Template.options) + assert_renders_correctly("eval_suppressed") + Haml::Template.options = {} + end + def test_exceptions_should_work_correctly template = <