public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Search Repo:
Allow for multiple non-literal functions that return hashes to be passed 
in the attributes hash for a Haml tag. Thanks to Evgeny Zislis for the 
idea.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@703 
7063305b-7217-0410-af8c-cdc13e5119b9
nex3 (author)
Wed Dec 19 01:41:33 -0800 2007
commit  efcb01e6a46b165c03e757e5fec45bc133802621
tree    971be5fbd391b726015ec0f8a726ec5829f38b72
parent  acda22070f1022eb3c3ab41dd5f37349715a6776
...
91
92
93
94
 
95
96
97
98
 
99
100
101
...
91
92
93
 
94
95
96
97
 
98
99
100
101
0
@@ -91,11 +91,11 @@ module Haml
0
 
0
     # Takes the various information about the opening tag for an
0
     # element, formats it, and adds it to the buffer.
0
- def open_tag(name, atomic, try_one_line, class_id, obj_ref, content, attributes_hash)
0
+ def open_tag(name, atomic, try_one_line, class_id, obj_ref, content, *attributes_hashes)
0
       tabulation = @real_tabs
0
       
0
       attributes = class_id
0
- if attributes_hash
0
+ attributes_hashes.each do |attributes_hash|
0
         attributes_hash.keys.each { |key| attributes_hash[key.to_s] = attributes_hash.delete(key) }
0
         self.class.merge_attrs(attributes, attributes_hash)
0
       end
...
431
432
433
434
 
435
436
437
 
438
439
440
441
 
442
443
444
...
480
481
482
 
483
484
485
...
499
500
501
502
 
503
504
505
...
510
511
512
513
 
514
515
516
...
523
524
525
526
 
 
527
528
529
...
431
432
433
 
434
435
436
 
437
438
439
440
 
441
442
443
444
...
480
481
482
483
484
485
486
...
500
501
502
 
503
504
505
506
...
511
512
513
 
514
515
516
517
...
524
525
526
 
527
528
529
530
531
0
@@ -431,14 +431,14 @@ END
0
     end
0
     
0
     def parse_static_hash(text)
0
- return {} unless text && inner = text.scan(/^\{(.*)\}$/)[0]
0
+ return {} unless text
0
 
0
       attributes = {}
0
- inner[0].split(',').each do |attrib|
0
+ text.split(',').each do |attrib|
0
         key, value, more = attrib.split('=>')
0
 
0
         # Make sure the key and value and only the key and value exist
0
- # Otherwise, it's too complicated and we'll defer it to the actual Ruby parser
0
+ # Otherwise, it's too complicated or dynamic and we'll defer it to the actual Ruby parser
0
         key = parse_literal_value key
0
         value = parse_literal_value value
0
         return nil if more || key.nil? || value.nil?
0
@@ -480,6 +480,7 @@ END
0
       raise SyntaxError.new("Invalid tag: \"#{line}\"") unless match = line.scan(TAG_REGEX)[0]
0
       tag_name, attributes, attributes_hash, object_ref, action, value = match
0
       value = value.to_s.strip
0
+ attributes_hash = attributes_hash[1...-1] if attributes_hash
0
 
0
       raise SyntaxError.new("Illegal element: classes and ids must have values.") if attributes =~ /[\.#](\.|#|\z)/
0
 
0
@@ -499,7 +500,7 @@ END
0
       object_ref = "nil" if object_ref.nil? || @options[:suppress_eval]
0
 
0
       static_attributes = parse_static_hash(attributes_hash) # Try pre-compiling a static attributes hash
0
- attributes_hash = "{nil}" if attributes_hash.nil? || static_attributes || @options[:suppress_eval]
0
+ attributes_hash = nil if static_attributes || @options[:suppress_eval]
0
       attributes = parse_class_and_id(attributes)
0
       Buffer.merge_attrs(attributes, static_attributes) if static_attributes
0
 
0
@@ -510,7 +511,7 @@ END
0
 
0
       atomic = true if !@block_opened && value.empty? && @options[:autoclose].include?(tag_name)
0
       
0
- if object_ref == "nil" && attributes_hash == "{nil}" && !flattened && (parse || Buffer.one_liner?(value))
0
+ if object_ref == "nil" && attributes_hash.nil? && !flattened && (parse || Buffer.one_liner?(value))
0
         # This means that we can render the tag directly to text and not process it in the buffer
0
         tag_closed = !value.empty? && Buffer.one_liner?(value) && !parse
0
 
0
@@ -523,7 +524,8 @@ END
0
       else
0
         flush_merged_text
0
         content = value.empty? || parse ? 'nil' : value.dump
0
- push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{atomic.inspect}, #{(!value.empty?).inspect}, #{attributes.inspect}, #{object_ref}, #{content}, #{attributes_hash[1...-1]})"
0
+ attributes_hash = ', ' + attributes_hash if attributes_hash
0
+ push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{atomic.inspect}, #{(!value.empty?).inspect}, #{attributes.inspect}, #{object_ref}, #{content}#{attributes_hash})"
0
       end
0
           
0
       return if atomic
...
299
300
301
 
 
 
 
 
 
302
303
304
...
299
300
301
302
303
304
305
306
307
308
309
310
0
@@ -299,6 +299,12 @@ class EngineTest < Test::Unit::TestCase
0
                  render("%p[user] New User", :locals => {:user => user}))
0
   end
0
 
0
+ def test_non_literal_attributes
0
+ assert_equal("<p a1='foo' a2='bar' a3='baz' />\n",
0
+ render("%p{a2, a1, :a3 => 'baz'}/",
0
+ :locals => {:a1 => {:a1 => 'foo'}, :a2 => {:a2 => 'bar'}}))
0
+ end
0
+
0
   def test_render_should_accept_a_binding_as_scope
0
     string = "This is a string!"
0
     string.instance_variable_set("@var", "Instance variable")

Comments

    No one has commented yet.