我希望编写一个可以“包装”子标记的标记,始终将所有属性传递给此子标记。因此,传递给父标记的任何属性都会传递给子标记。我们的想法是生成一个标签,可以“装饰”多个不同的子标签,从而在不改变现有标签的情况下扩展其功能。
这可能在grails中吗?
这个想法的大致轮廓:
// view
<mytaglib:ParentTag attr1="hello" attr2="world">
</mytaglib:childTag1>
</mytaglib:ParentTag
<mytaglib:ParentTag attr1="hello" attr2="world">
</mytaglib:childTag2>
</mytaglib:ParentTag
在.gsp:
中<div class='parent'>
<div attr1="hello" attr2="world"></div>
</div>
<div class='parent'>
<div attr1="hello" attr2="world"></div>
</div>
生成的HTML:
ContentResolver cr = ContentResolver;
string contactName = null;
var cur = cr.Query(ContactsContract.Contacts.ContentUri,null,null,null,null);
if (cur.MoveToFirst())
{
do
{
string id = cur.GetString(cur.GetColumnIndex(BaseColumns.Id));
contactName = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
var emails = cr.Query(ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId + " = " + id, null, null);
if (emails.MoveToFirst()) {
do
{
// This is where it loops through if there are multiple Email addresses
var email = emails.GetString(emails.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data));
} while (emails.MoveToNext());
}
} while (cur.MoveToNext());
}
答案 0 :(得分:1)
def parentTag = {attrs, body ->
// do your decoration stuff
out << "<div>"
// call the child tag
out << childTag(attrs, body)
// do your decoration stuff
out << "</div>"
}
def childTag = {attrs, body ->
out << "<div attr1='" + attrs['attr1'] + "' attr2='" + attrs['attr2'] + "'></div>"
}