Local variables can now be set via Haml::Engine, rather than solely via Haml::Template.

git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@114 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-11-04 08:35:06 +00:00
parent 00ef3f9983
commit 6c8ff1c515
4 changed files with 47 additions and 24 deletions

View File

@ -549,6 +549,11 @@ from <tt>environment.rb</tt>. Available options are:
(e.g. by replacing them with <tt>&apos;</tt>) if
the character is an apostrophe or a quotation mark.
[<tt>:locals</tt>] The local variables that will be available within the
template. For instance, if <tt>:locals</tt> is
<tt>{ :foo => "bar" }</tt>, then within the template,
<tt>= foo</tt> will produce <tt>bar</tt>.
---
Copyright (c) 2006 Hampton Catlin
Licensed under the MIT License

View File

@ -96,7 +96,8 @@ module Haml
def initialize(template, options = {})
@options = {
:suppress_eval => false,
:attr_wrapper => "'"
:attr_wrapper => "'",
:locals => {}
}.merge options
@precompiled = @options[:precompiled]
@ -119,6 +120,16 @@ module Haml
@scope_object = scope
@buffer = Haml::Buffer.new(@options)
local_assigns = @options[:locals]
# Get inside the view object's world
@scope_object.instance_eval do
# Set all the local assigns
local_assigns.each do |key,val|
self.class.send(:define_method, key) { val }
end
end
# Compile the @precompiled buffer
compile &block

View File

@ -4,15 +4,15 @@ 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
@ -38,20 +38,21 @@ module Haml
assigns.each do |key,val|
instance_variable_set "@#{key}", val
end
# Set all the local assigns
local_assigns.each do |key,val|
self.class.send(:define_method, key) { val }
end
end
options = @@options.dup
locals = options[:locals] || {}
locals.merge! local_assigns
options[:locals] = locals
if @precompiled = get_precompiled(template_file_name)
options = { :precompiled => @precompiled }.merge @@options
options[:precompiled] ||= @precompiled
engine = Haml::Engine.new("", options)
else
engine = Haml::Engine.new(File.read(template_file_name), @@options)
engine = Haml::Engine.new(File.read(template_file_name), options)
set_precompiled(template_file_name, engine.precompiled)
end
yield_proc = @view.instance_eval do
proc { |*name| instance_variable_get("@content_for_#{name.first || 'layout'}") }
end

View File

@ -8,20 +8,11 @@ class EngineTest < Test::Unit::TestCase
def render(text, options = {})
Haml::Engine.new(text, options).to_html
end
def test_empty_render_should_remain_empty
assert_equal('', render(''))
end
def test_stop_eval
assert_equal("", render("= 'Hello'", :suppress_eval => true))
end
def test_attr_wrapper
assert_equal("<p strange=*attrs*>\n</p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
assert_equal("<p escaped=\"quo&quot;te\">\n</p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
end
# This is ugly because Hashes are unordered; we don't always know the order
# in which attributes will be returned.
# There is probably a better way to do this.
@ -45,7 +36,7 @@ class EngineTest < Test::Unit::TestCase
assert_equal("<div class='stripped'>This should have no spaces in front of it</div>",
render(".stripped This should have no spaces in front of it").chomp)
end
def test_one_liner_should_be_one_line
assert_equal("<p>Hello</p>", render('%p Hello').chomp)
end
@ -53,11 +44,26 @@ class EngineTest < Test::Unit::TestCase
def test_long_liner_should_not_print_on_one_line
assert_equal("<div>\n #{'x' * 51}\n</div>", render("%div #{'x' * 51}").chomp)
end
def test_multi_render
engine = Haml::Engine.new("%strong Hi there!")
assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
end
# Options tests
def test_stop_eval
assert_equal("", render("= 'Hello'", :suppress_eval => true))
end
def test_attr_wrapper
assert_equal("<p strange=*attrs*>\n</p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
assert_equal("<p escaped=\"quo&quot;te\">\n</p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
end
def test_locals
assert_equal("<p>Paragraph!</p>\n", render("%p= text", :locals => { :text => "Paragraph!" }))
end
end