Search code examples
grailsgsp

Keeping data retrieved in main layout


I have a main.gsp and other .gsp pages that use the <meta name="layout" content="main" />.

One of the pages is page1.gsp and it has a page1Controller that retrieve a list of links from endpoint.

In main.gsp, the list of links retrieved are shown, so supposedly any pages that use should show the list.

Is there anyway other than putting the retrieve list function in every one of those controllers?


Solution

  • I recommend you to use a taglib with a template.

    MyTaglib.groovy

    class MyTagLib {
    
        static defaultEncodeAs = [taglib:'html']
        static namespace = 'myTaglib'
    
        def links = { attrs, body ->
            // your logic to retrieve links or whatever
            out << render(template: 'links', model: [links:links])
        }
    }
    

    _links.gsp

    ${links} <!-- display your links -->
    

    page1.gsp (or any other gsp)

    <myTaglib:links />