用Grails包装另一个标签的标签?

时间:2015-08-11 13:41:17

标签: grails gsp

我希望编写一个可以“包装”子标记的标记,始终将所有属性传递给此子标记。因此,传递给父标记的任何属性都会传递给子标记。我们的想法是生成一个标签,可以“装饰”多个不同的子标签,从而在不改变现有标签的情况下扩展其功能。

这可能在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());
        }

1 个答案:

答案 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>"
}
相关问题