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:
parent
3e0156a858
commit
f79bddb73e
6 changed files with 56 additions and 19 deletions
19
REFERENCE
19
REFERENCE
|
@ -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>'</tt>) if
|
||||
the character is an apostrophe or a quotation mark.
|
||||
|
||||
---
|
||||
Copyright (c) 2006 Hampton Catlin
|
||||
|
|
|
@ -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>'</tt>) if
|
||||
# the character is an apostrophe or a quotation mark.
|
||||
|
||||
def initialize(template, options = {})
|
||||
@options = {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
2
test/results/eval_suppressed.xhtml
Normal file
2
test/results/eval_suppressed.xhtml
Normal file
|
@ -0,0 +1,2 @@
|
|||
<p></p>
|
||||
<h1>Me!</h1>
|
|
@ -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
|
||||
|
|
5
test/templates/eval_suppressed.haml
Normal file
5
test/templates/eval_suppressed.haml
Normal file
|
@ -0,0 +1,5 @@
|
|||
= "not me!"
|
||||
= "nor me!"
|
||||
- foo = "not even me!"
|
||||
%p= foo
|
||||
%h1 Me!
|
Loading…
Reference in a new issue