问题
Is posibble to have something like this? :
%div{"data-regex": "a/regular/expression"}
When I try to do this ways, I get this error:
syntax error, unexpected ':', expecting tASSOC
I tried this: %div{"data-regex": #{"a/regular/expression"}}
, but is the same.
回答1:
What you're probably looking for is:
%div{data: {regex: "a/regular/expression"} }
However it would be nice if you included the desired HTML in your question so we could know for sure. The other answer provided will also work, but this is especially nice if you want to provide many data attributes without repeating "data-" all over the place. That is, you can do:
%div{data: {regex: "a/reg/ex", attr2: "something", attr3: "something else" } }
Note, your problem is that the nice syntax in Ruby 1.9+ for Symbol keys in hashes doesn't work with strings preceding the colon.
{ a: 123 }
# => { :a => 123 }
{ :"a" => 123 }
# => { :a => 123 }
{ "a" => 123 }
# => { "a" => 123 }
{ "a": => 123 }
# => SyntaxError ...
回答2:
To make sure that it works, you can try usual form to write a hash of parameters:
%div{:'data-regex' => "a/regular/expression"}
I guess, this may be applied to use in ruby 2.x:
%div{"data-regex": "a/regular/expression"}
来源:https://stackoverflow.com/questions/21472655/regular-expression-in-haml-html-attribute