Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using out << in Grails taglib

Tags:

grails

groovy

In a Grails taglib I have seen:

out << 'some html'

and I've also seen:

out << body() << 'some html'

What is the difference?

Thanks

like image 498
RyanLynch Avatar asked Aug 12 '11 06:08

RyanLynch


1 Answers

The first outputs the string to the response. The second renders the body of the tag to the response and then also renders the string. Not all tags have a body, so the 2nd one is more rare. For example <g:img src='foo.gif'/> doesn't have a body but <g:ifLoggedIn>Welcome Back</g:ifLoggedIn> does.

When you define a tag closure, it either has one argument, typically 'attrs', or two, typically 'attrs' and 'body'. The names don't matter, they're just sensible defaults. The body argument will be a closure that renders the inner content of the tag when it's invoked like a method (i.e. body())

like image 56
Burt Beckwith Avatar answered Sep 29 '22 01:09

Burt Beckwith