{"id":21,"date":"2008-09-12T20:01:50","date_gmt":"2008-09-12T14:31:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21"},"modified":"2015-07-10T13:35:48","modified_gmt":"2015-07-10T08:05:48","slug":"how-to-pass-a-bean-or-model-to-a-template","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-pass-a-bean-or-model-to-a-template\/","title":{"rendered":"How to pass a bean or model to a template"},"content":{"rendered":"<p>When we want to render a view using a template and the template makes use of a model object, the model object needs to be passed to the template using GSP tags. I am writing this because it took me a while to figure this out. Thanks to the wonderful contributors on the mailing list who helped me with this.<\/p>\n<p><span style=\"text-decoration: underline;\">To render the view to a template from a <strong>gsp<\/strong> we need to use this tag in your <strong>gsp<\/strong><\/span><\/p>\n<p>&lt;g:render template=&#8221;\/templateName&#8221;\/&gt;<\/p>\n<p><span style=\"text-decoration: underline;\">To send a bean to the template we have to include\u00a0 an attribute to the tag like this<\/span><\/p>\n<p>&lt;g:render template=&#8221;\/templateName&#8221; bean=&#8221;beanName&#8221;\/&gt;<\/p>\n<p><span style=\"text-decoration: underline;\">(or) we can also use a named variable in the template.For this we have to send a model<\/span><\/p>\n<pre>&lt;g:render template=\"\/templateName\" model=\"beanName:${someBeanName}\"\/&gt;<\/pre>\n<p>In the template we can use this by calling &#8216;it&#8217;\u00a0 but that&#8217;s not my preferred way. The other way is, assign this value to a variable in the template like this.<\/p>\n<pre>&lt;g:set var=\"variableName\" value=\"${beanName}\"\/&gt;<\/pre>\n<p>Now we can use the variableName in the template.<\/p>\n<p><span style=\"text-decoration: underline;\">Example<\/span>:<\/p>\n<pre>&lt;input type=\"text\" id=\"fieldId\" class=\"${hasErrors(bean: variableName, field:\r\n'fieldName', 'errors')}\" name=\"fieldName\" value=\"${fieldValue(bean: variableName,\r\nfield: 'fieldName')}\"\/&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When we want to render a view using a template and the template makes use of a model object, the model object needs to be passed to the template using GSP tags. I am writing this because it took me a while to figure this out. Thanks to the wonderful contributors on the mailing list [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[4840,9,32],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}