ufucuk
ufucuk

Reputation: 475

Grails nested tags

I have been searching all over the internet also in documentations about nested tags for parent/child relation but I got nothing so far. What I want to learn is, is there anyway for such a custom "parent" tag to know about its "children"

<mytag:parent source="${somelist}">
    <mytag:child column="name" style="padding-left:10px">
    </mytag>
    <mytag:child column="surname" style="padding-left:10px">
    </mytag>
</mytag>

In this example parent tag gets the collection and children prints their given columns by calling somelist.name and somelist.surname respectively. Do I have to parse the DOM to learn about children or can I reach the children somehow in "groovy" code?

Upvotes: 2

Views: 807

Answers (1)

Igor Artamonov
Igor Artamonov

Reputation: 35961

<mytag:parent> should put own context/data as a request scope attribute (or page scope), process it by <mytag:child>, and remove it on closing tag. Like:

static final CONTEXT = this.class.name

def parent = { attrs, body ->
    def data = [
        name: 'test 1',
        surname: 'test 2'  // i guess you want to load this values from attr.source
    ]
    request.setAttribute(CONTEXT, data)
    out << body.call()
    request.removeAttribute(CONTEXT)
}


def child = { attrs, body ->
   def data = request.getAttribute(CONTEXT)
   out << 'name: '
   out << data.name  
   out << 'surname: '
   out << data.surname  
}

Upvotes: 5

Related Questions