简体   繁体   中英

g:set tag inside template body in grails

I using <g:set> tag inside g:render template body

<g:render template="/template/panelContainer">
  <g:set var="tomorrow" value="${new Date() + 1}"/>
  ${tomorrow}
</g:render>

_panelContainer.gsp:

<div class="panel panel-default">
${raw(body())}
</div>

However tomorrow variable is not set. How do I need to implement template to properly execute g:set tag?

First of all you need to set the variable before calling the render method.

<g:set var="tomorrow" value="${new Date() + 1}"/>

Then you can pass this variable in your template using the model attribute. Like:

<g:render template="/template/panelContainer" model="${[tomorrow:tomorrow]}"/>

The body() you are trying to use is used to pass along other body content to be displayed inside the template. Like:

<g:render template="/template/panelContainer" model="${[tomorrow:tomorrow]}">
  <a href="..">..</a>
</g:render>

Try this :

<g:set var="tomorrow" value="${new Date() + 1}"/>
<g:render template="/template/panelContainer" model="${[tomorrow:tomorrow]}"/>

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