开发者

HAML meta-programming

开发者 https://www.devze.com 2023-03-07 07:18 出处:网络
Let\'s say I want to render the following <div id=\"foo\" class=\"f开发者_如何学Pythonoo\">

Let's say I want to render the following

<div id="foo" class="f开发者_如何学Pythonoo">
  Bar
</div>

In HAML, I'd typically write

#foo.foo Bar

Is there an easy way to do something like the following?

#foo{ :class => id_for_this_tag }

What I really want is the ability to hook into the markup generation of HAML to clean up some views.


The code in your example should work as is. The following line:

#foo{:class => @my_class }

Will generate the following HTML:

<div class='foobar' id='foo'></div>

Assuming that @my_class = "foobar" is defined appropriately.

I'm not aware of any way to trim it any further, but you can get away with anything using this format. For example:

- bob = "silly"
#foo{:class => bob, :summary => "whatever", :arbitrary => "arbitrary"}

Generates:

<div arbitrary='arbitrary' class='silly' id='foo' summary='whatever'>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号