{"id":2739,"date":"2011-02-10T11:09:04","date_gmt":"2011-02-10T05:39:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2739"},"modified":"2016-12-19T15:19:23","modified_gmt":"2016-12-19T09:49:23","slug":"modifying-grails-scaffolded-templates-i","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/modifying-grails-scaffolded-templates-i\/","title":{"rendered":"Modifying Grails Scaffolded templates &#8211; I"},"content":{"rendered":"<p>In one of the sessions in SpringOne2GX, there was a session on Uber-Scaffolding by Jean Barmarsh. The session was quite incredible and opened up a world of possibilities. We all know that the scaffolded code generated by grails is modifiable if we install the base templates. This is done simply by saying:<\/p>\n<p>[java]grails install-templates[\/java]<\/p>\n<p>The above command will create the following directory structure in the application&#8217;s src folder<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2011\/02\/grailsTemplates.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2742 aligncenter\" title=\"grailsTemplates\" src=\"\/blog\/wp-ttn-blog\/uploads\/2011\/02\/grailsTemplates.png\" alt=\"\" width=\"272\" height=\"598\" \/><\/a><br \/>\nAs we can see, there are 3 main directories here :<\/p>\n<ol>\n<li>Artifacts: This directory contains all the base files for creating various artifacts. For example, if you say &#8220;grails create-domain-class packageName.className&#8221;, then the file &#8220;src\/artifacts\/DomainClass.groovy&#8221; is taken as the template for creating this file. Same goes for other artifacts like filters, taglibs, controllers, etc.<\/li>\n<li>Scaffolding: This directory contains the template files that are used for generating scaffolded code(controllers and views).<\/li>\n<li>War : This just contains the web.xml file. If you need to add some configuration block(e.g. session-timeout, etc)<\/li>\n<\/ol>\n<p>Now there are a lot of things that we may need to modify in the grails templates, the pagination size being one. So, the first change that I did was made the &#8220;max&#8221; parameter in controller be Config driven simply by replacing :<\/p>\n<p>[java]<\/p>\n<p>params.max = Math.min(params.max ? params.int(&#8216;max&#8217;) : 10, 100)<\/p>\n<p>[\/java]<\/p>\n<p>by<\/p>\n<p>[java]<\/p>\n<p>int pageSize = grailsApplication.config.grails.scaffolds.pageSize ?: 10<br \/>\nparams.max = Math.min(params.max ? params.int(&#8216;max&#8217;) : pageSize, 100)<\/p>\n<p>[\/java]<\/p>\n<p>This was the simplest thing that was done.<\/p>\n<p>In order to use the changed scaffolding, there are two ways of doing this:<\/p>\n<ol>\n<li>[java]grails generate-all MyDomainClass[\/java]<\/li>\n<li>Create an empty controller, and put the following code in it : [java]def scaffold = MyDomainClass[\/java]<\/li>\n<\/ol>\n<blockquote><p><strong>Note<\/strong>: When playing around with grails templates, be very careful not  to format this code using an IDE. The formatting  will add unwanted  spaces and the grails create-*\/generate-* commands will start  failing  or produce wrong\/unusable files.<\/p><\/blockquote>\n<p>Will be back with more \ud83d\ude42<\/p>\n<p>Regards<br \/>\n~~Himanshu Seth~~<\/p>\n<p>http:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of the sessions in SpringOne2GX, there was a session on Uber-Scaffolding by Jean Barmarsh. The session was quite incredible and opened up a world of possibilities. We all know that the scaffolded code generated by grails is modifiable if we install the base templates. This is done simply by saying: [java]grails install-templates[\/java] The [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[4840,514],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2739"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2739"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2739\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}