{"id":633,"date":"2010-05-03T14:43:41","date_gmt":"2010-05-03T09:13:41","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=633"},"modified":"2016-12-19T15:28:30","modified_gmt":"2016-12-19T09:58:30","slug":"injecting-method-to-trim-string-length-on-gsp-pages","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/injecting-method-to-trim-string-length-on-gsp-pages\/","title":{"rendered":"Injecting Method to trim string length on gsp pages"},"content":{"rendered":"<p>Restricting the length of a string on gsps is a common scenario in most of our projects.<br \/>\nGroovy&#8217;s metaprogramming magic made it too simple for me. I added the following lines of code to Bootstrap.groovy:<\/p>\n<pre lang=\"groovy\">\r\nObject.metaClass.trimLength = {Integer stringLength ->\r\n\r\n    String trimString = delegate?.toString()\r\n    String concatenateString = \"...\"\r\n    List separators = [\".\", \" \"]\r\n\r\n    if (stringLength && (trimString?.length() > stringLength)) {\r\n        trimString = trimString.substring(0, stringLength - concatenateString.length())\r\n        String separator = separators.findAll{trimString.contains(it)}?.min{trimString.lastIndexOf(it)}\r\n        if(separator){\r\n            trimString = trimString.substring(0, trimString.lastIndexOf(separator))\r\n        }\r\n        trimString += concatenateString\r\n    }\r\n    return trimString\r\n}\r\n<\/pre>\n<p>Now, for an instance of class Person: <\/p>\n<pre lang=\"groovy\">\r\nclass Person {\r\n    String name\r\n\r\n    String toString(){\r\n        return name\r\n    }\r\n}\r\n\r\nPerson person = new Person(name: \"Aman Aggarwal\")\r\n<\/pre>\n<p>I can simply write this code in gsp: <\/p>\n<pre lang=\"groovy\">\r\nperson.trimLength(10)\r\n<\/pre>\n<p>which renders output:<\/p>\n<pre lang=\"groovy\">\r\nAman...\r\n<\/pre>\n<p>Hope it helps.<\/p>\n<p>~Aman Aggarwal<br \/>\naman@intelligrape.com<\/p>\n<p>http:\/\/www.tothenew.com\/blog\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Restricting the length of a string on gsps is a common scenario in most of our projects. Groovy&#8217;s metaprogramming magic made it too simple for me. I added the following lines of code to Bootstrap.groovy: Object.metaClass.trimLength = {Integer stringLength -> String trimString = delegate?.toString() String concatenateString = &#8220;&#8230;&#8221; List separators = [&#8220;.&#8221;, &#8221; &#8220;] if [&hellip;]<\/p>\n","protected":false},"author":282,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[4840,9,32,145],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/633"}],"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\/282"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}