Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping Grails render XML builder naming conflicts

Tags:

grails

groovy

For example:

def list = {
    books = Book.findAll(...)
    render(contentType: 'text/xml') {
        books(count: books.size()) {
            for(book in books) {
                book(id: book.id) {
                    title(book.title)
                }
            }
        }
    }
}

Using books and book as nodes cause naming conflicts. I understand I can rename everything to prevent the issue but is there a way to escape the nodes to prevent the issue and keep a clean naming convention?

like image 391
James Allman Avatar asked Jul 09 '26 18:07

James Allman


1 Answers

Hubert Klein Ikkink has posted a blog entry with an alternate solution:

Groovy Goodness: Solve Naming Conflicts with Builders

He recommends prepending delegate to the node names so that example would become:

def list = {
    books = Book.findAll(...)
    render(contentType: 'text/xml') {
        delegate.books(count: books.size()) {
            for(book in books) {
                delegate.book(id: book.id) {
                    title(book.title)
                }
            }
        }
    }
}
like image 105
James Allman Avatar answered Jul 14 '26 16:07

James Allman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!