如何在Haml中制作动态ID? [英] How do I make dynamic ids in Haml?
本文介绍了如何在Haml中制作动态ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#item
创建一个id为"item"的div
creates a div with id="item"
.box#item
使用class ="box"和id ="item"
creates a div with class="box" and id="item"
.box#="item "+x
创建一个带有class ="box"和注释'#="item" + x"
creates a div with class="box" and a comment '#="item"+x'
.box#
="item"+x
抛出非法元素:类和ID必须具有值."
throws "Illegal element: classes and ids must have values."
如何将ID设置为变量?
How do I get set the id to a variable?
推荐答案
有两种方法:
长格式(将id定义为常规属性):
The long form way (define the id as if it were a regular attribute):
.box{:id => "item_#{x}"}
产生此结果(x
是x.to_s
求值的结果):
produces this (x
is what ever x.to_s
evaluates to):
<div class="box" id="item_x">
简写方式:
.box[x]
假设x
是item
的实例,
会产生以下内容:
produces the following assuming x
is an instance of item
:
<div class="box item" id="item_45">
有关更多信息,请参见 HAML参考.
See the HAML reference for more information.
这篇关于如何在Haml中制作动态ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文