1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Options can now be set from environment.rb. See REFERENCE.

git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@106 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-10-30 06:59:57 +00:00
parent 3e0156a858
commit f79bddb73e
6 changed files with 56 additions and 19 deletions

View file

@ -514,7 +514,26 @@ may be compiled to:
<a href='/'>Home</a>
</div>
</div>
=== Setting Options
Options can be set by setting the hash <tt>Haml::Template.options</tt>
from <tt>environment.rb</tt>. Available options are:
[<tt>:suppress_eval</tt>] Whether or not attribute hashes and Ruby scripts
designated by <tt>=</tt> or <tt>~</tt> should be
evaluated. If this is true, said scripts are
rendered as empty strings. Defaults to false.
[<tt>:precompiled</tt>] A string containing a precompiled Haml template.
If this is passed, <tt>template</tt> is ignored
and no precompilation is done.
[<tt>:attr_wrapper</tt>] The character that should wrap element attributes.
This defaults to <tt>'</tt> (an apostrophe). Characters
of this type within the attributes will be escaped
(e.g. by replacing them with <tt>&apos;</tt>) if
the character is an apostrophe or a quotation mark.
---
Copyright (c) 2006 Hampton Catlin

View file

@ -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:
#
# [<tt>:suppress_eval</tt>] Whether or not attribute hashes and Ruby scripts
# designated by <tt>=</tt> or <tt>~</tt> should be
# evaluated. If this is true, said scripts are
# rendered as empty strings. Defaults to false.
# template string. See REFERENCE for available options.
#
# [<tt>:precompiled</tt>] A string containing a precompiled Haml template.
# If this is passed, <tt>template</tt> is ignored
# and no precompilation is done.
#--
# When adding options, remember to add information about them
# to REFERENCE!
#++
#
# [<tt>:attr_wrapper</tt>] The character that should wrap element attributes.
# This defaults to <tt>'</tt> (an apostrophe). Characters
# of this type within the attributes will be escaped
# (e.g. by replacing them with <tt>&apos;</tt>) if
# the character is an apostrophe or a quotation mark.
def initialize(template, options = {})
@options = {

View file

@ -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

View file

@ -0,0 +1,2 @@
<p></p>
<h1>Me!</h1>

View file

@ -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 = <<END
%p

View file

@ -0,0 +1,5 @@
= "not me!"
= "nor me!"
- foo = "not even me!"
%p= foo
%h1 Me!