Kay
Kay

Reputation: 33

Sending data to "View" from "Controller"

In my controller class have the following code

class MyController {

    def flickrService

    def index = {
        def data = flickrService.search {
            tags 'tag,tag2,tag3'
            page 3
            perPage 14 // Look ma!
        }
        [urls:data.urls,page:data.page,pages:data.pages]
    }

}

I have also created an index.gsp file. As I am new to groovy grails - i could not figure it out how to access data returned by flickrservice in the view. Can I just access "data" defined above in the index view or I need to set it in the controller before I can loop through the returned data? Any help would be highly appreciated. Thanks

Upvotes: 3

Views: 5655

Answers (1)

Hoàng Long
Hoàng Long

Reputation: 10848

Yes, now you can access data from the view, for example,in index.gsp:

<html><head>Test</head><body>${urls} <br/> ${page} </body></html>

Generally saying, grails return the last value in function by default, so if you want to access many data, you can do like this:

class MyController {

    def flickrService

    def index = {
        def data = ...
        def data1 = ...
        def data2 = ...

        // Here's the return result:
            [view_data:data,view_data1:data1, view_data2:data2]
    }

}

Then you can access ${view_data},${view_data1},${view_data2} in view.

Upvotes: 5

Related Questions