简体   繁体   中英

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM