{"id":8538,"date":"2012-09-25T02:58:05","date_gmt":"2012-09-24T21:28:05","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8538"},"modified":"2012-09-25T02:58:05","modified_gmt":"2012-09-24T21:28:05","slug":"inject-methodsproperties-to-be-available-in-all-gsp-pages","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/inject-methodsproperties-to-be-available-in-all-gsp-pages\/","title":{"rendered":"Inject Methods\/Properties to be available in all GSP Pages"},"content":{"rendered":"<p>Hi,<\/p>\n<p>&nbsp;<\/p>\n<p>There could be cases where we would need to inject some methods or properties in all GSP pages. <\/p>\n<p>&nbsp;<\/p>\n<p>As we know that the <em><strong>GSP pages extends GroovyPage class<\/strong><\/em>, hence injecting properties\/methods in GroovyPage class can solve out purpose.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example:<\/strong> Lets add appVersion property and a method to provide logo link irrespective of the knowledge of containing module.<br \/>\n[java]<br \/>\n\/\/Adding property<br \/>\nGroovyPage.metaClass.appVersion = &quot;my app Version&quot;<\/p>\n<p>\/\/Adding the method to give the logo link<br \/>\nGroovyPage.metaClass.logoLink = {<br \/>\n        resource(dir: &#8216;images&#8217;, file: &#8216;logo.png&#8217;,plugin: &#8216;my-module-containing-logo&#8217;)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>Now in Any GSP in the application we can do:<br \/>\n[html]<br \/>\n&lt;div&gt;<br \/>\n&lt;img src=&quot;${logoLink()}&quot; \/&gt;<br \/>\n&lt;p&gt;App Version:- ${appVersion}&lt;\/p&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>[\/html]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Hope It Helps!<br \/>\n<strong>Kushal Likhi<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, &nbsp; There could be cases where we would need to inject some methods or properties in all GSP pages. &nbsp; As we know that the GSP pages extends GroovyPage class, hence injecting properties\/methods in GroovyPage class can solve out purpose. &nbsp; Example: Lets add appVersion property and a method to provide logo link irrespective [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[9,1069,145],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8538"}],"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\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8538"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8538\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}