Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically add an attribute to a body element in a GSP page

Tags:

grails

gsp

In Grails 2.3.7, is there a way to use an expression in a GSP page to add an attribute to a body element? In the code below, the expression in the p element works, but the same expression in the body element causes a error: Expecting '=' after attribute name (${raw('this="that"')}).

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
    </head>
   <body ${raw('this="that"')}>
        <p ${raw('this="that"')}>Hello!</p>
    </body>
</html>

I'm trying to do this in a layout and pick up the corresponding attribute from the original page with pageProperty, but the same error occurs on the body element in the page layout as well.

Replacing an attribute value does work in a body element like this:

<body this="${that}">

but this won't work because I do not want the attribute to appear at all if it has no value.

like image 368
Aoife Avatar asked Dec 01 '25 02:12

Aoife


1 Answers

the problem is, that the body tag is replaced by the <g:layoutBody /> tag and therefore could not be set like this.

One solution is to use to set different stuff in the sitemesh layout. An example of this is shown here:

<html>
<head>
    <g:layoutHead/>
</head>
<body class="${pageProperty( name:'body.class' )}">
    <g:layoutBody/>
</body>
</html>
like image 65
Mario David Avatar answered Dec 03 '25 22:12

Mario David



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!