regular expression in haml html attribute

ε祈祈猫儿з 提交于 2019-12-25 16:44:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!