Code Generation With Grail-s Templates or Custom Scripts

87 views
Skip to first unread message

Saleh Mamdouh

unread,
Jun 17, 2016, 12:20:18 PM6/17/16
to Grails Dev Discuss
Hi,

I have been asking this on git-hub.
The generated code for GRails, generates all fields by a single call to <f:all bean="whatever" />. The GSPs generated from a template when you invoke grails generate-all is just a mere copy of the template files. Only difference is that they just replace the bean name in the generated gsp-s.

Is there any documentation or code examples of how I can use the Grails templates or custom scripts to generated code ?
I will try to write a detailed example here :

User (name, id, dob, age). When I do grails generate-view User, I want the generated gsp to contains

<gsp:field bean="user" property="id" />
<gsp:field bean="user" property="dob" />
<gsp:field bean="user" property="age" />
<gsp:field bean="user" property="name" />

I want the code generated depends on the attributes of the bean.I have told on the github it is possible, but that doesn't seem to be true.
Any thoughts ?

Jeff Brown

unread,
Jun 19, 2016, 2:25:31 PM6/19/16
to grails-de...@googlegroups.com
On June 17, 2016 at 11:20:35 AM, Saleh Mamdouh
(salehmam...@gmail.com) wrote:
> Hi,
>
> I have been asking this on git-hub.
> The generated code for GRails, generates all fields by a single call to
> . The GSPs generated from a template when you
> invoke grails generate-all is just a mere copy of the template files. Only
> difference is that they just replace the bean name in the generated gsp-s.
>
> Is there any documentation or code examples of how I can use the Grails
> templates or custom scripts to generated code ?
> I will try to write a detailed example here :
>
> User (name, id, dob, age). When I do grails generate-view User, I want the
> generated gsp to contains
>
>
>
>
>
>
> I want the code generated depends on the attributes of the bean.I have told
> on the github it is possible, but that doesn't seem to be true.
> Any thoughts ?

You can customize the templates generated by “install-templates” so
you get to define whatever is generated when you run “generate-all” or
“generate-views” etc.  I don’t think there is enough information in
the model to do specifically what you are wanting to do to though.  I
think that would require changing the current “generate-*” commands or
writing your own.



JSB

--
Jeff Scott Brown
OCI Grails Practice Lead
Principal Software Engineer

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

Victor Kakama

unread,
Jun 20, 2016, 5:32:19 AM6/20/16
to Grails Dev Discuss
Hi,
Currently Grails 3.1.8 seems to ignore the installed Templates,
An example is customising an index.gsp template after installing templates.
On generating views it seems to ignore the custom changes.

Jeff Brown

unread,
Jun 20, 2016, 8:03:22 AM6/20/16
to grails-de...@googlegroups.com
On June 20, 2016 at 4:32:36 AM, Victor Kakama (vka...@gmail.com) wrote:
> Hi,
> Currently Grails 3.1.8 seems to ignore the installed Templates,
> An example is customising an index.gsp template after installing templates.
> On generating views it seems to ignore the custom changes.
>

That is not what I am seeing.

See the Grails 3.1.8 project at https://github.com/jeffbrown/scafftemplates.

Notice the customized header at
https://github.com/jeffbrown/scafftemplates/blob/bacbcf97ca8a6a4f1184072bd9d01ef3fa7e8aec/src/main/templates/scaffolding/index.gsp#L16.

The CarController is using dynamic scaffolding and when I access the
index action in that controller I see the custom template being used
for the rendered view.  Also when I run “grails generate-all
demo.Person”, the corresponding generated view also appears to use the
custom template.

Jeff Brown

unread,
Jun 20, 2016, 9:46:09 AM6/20/16
to grails-de...@googlegroups.com


On June 20, 2016 at 7:03:20 AM, Jeff Brown (bro...@ociweb.com) wrote:
>
>
> On June 20, 2016 at 4:32:36 AM, Victor Kakama (vka...@gmail.com) wrote:
> > Hi,
> > Currently Grails 3.1.8 seems to ignore the installed Templates,
> > An example is customising an index.gsp template after installing templates.
> > On generating views it seems to ignore the custom changes.
> >
>
> That is not what I am seeing.
>
> See the Grails 3.1.8 project at https://github.com/jeffbrown/scafftemplates.
>
> Notice the customized header at https://github.com/jeffbrown/scafftemplates/blob/bacbcf97ca8a6a4f1184072bd9d01ef3fa7e8aec/src/main/templates/scaffolding/index.gsp#L16.
>
> The CarController is using dynamic scaffolding and when I access the index action in
> that controller I see the custom template being used for the rendered view. Also when
> I run “grails generate-all demo.Person”, the corresponding generated view also appears
> to use the custom template.


Are the templates being ignored on Windows?

Victor Kakama

unread,
Jun 20, 2016, 12:21:28 PM6/20/16
to Grails Dev Discuss
Thanx Jeff,
On re creating the project and upgrading my gradle version i got it working.


On Friday, 17 June 2016 19:20:18 UTC+3, Saleh Mamdouh wrote:

Saleh Mamdouh

unread,
Jun 21, 2016, 1:02:37 PM6/21/16
to grails-de...@googlegroups.com

Hi,

Will also post sub-question on the Slack channel.
Actually, generating the code I need does not seem to be that difficult. However, from within the custom script, I do not have access to the grailsApplicaiton implicit object nor can I access the domain classes. Although I import domain classes, I get a runtime error when running the script saying that class cannot be resolved.

Below code works well from a controller method :
def gdClass = grailsApplication.getDomainClass(Student.name)
def props = gdClass.persistentProperties as List

for (property in props) {
println "+++++++++++++++++++++++++++"
println property.name
}
THe code breaks if I put it in a custom script. I am not sure if that what @Jeff you mean that I do not
have access to the class ?


--
You received this message because you are subscribed to a topic in the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grails-dev-discuss/mYXaV4NZAM8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/b57f8602-b6d1-4a70-9836-ab6824087233%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jeff Brown

unread,
Jun 21, 2016, 3:41:45 PM6/21/16
to grails-de...@googlegroups.com


On June 21, 2016 at 12:02:58 PM, Saleh Mamdouh (salehmam...@gmail.com) wrote:
> Hi,
>
> Will also post sub-question on the Slack channel.
> Actually, generating the code I need does not seem to be that difficult.
> However, from within the custom script, I do not have access to the
> grailsApplicaiton implicit object nor can I access the domain classes.
> Although I import domain classes, I get a runtime error when running the
> script saying that class cannot be resolved.
>
> Below code works well from a controller method :
>
> def gdClass = grailsApplication.getDomainClass(Student.name)
> def props = gdClass.persistentProperties as List
>
> for (property in props) {
> println "+++++++++++++++++++++++++++"
> println property.name
> }
>
> THe code breaks if I put it in a custom script. I am not sure if that
> what @Jeff you mean that I do not
>
> have access to the class ?
>


Your options really depend on what you need to access and what you want to do with whatever it is you need access to.  One option you have is if you write a custom ApplicationCommand, you can have the GrailsApplication instance auto wired right into the command and then navigate from there.  Commands take longer to initialize, but that is one option.

import grails.core.GrailsApplication
import grails.dev.commands.ApplicationCommand
import grails.dev.commands.ExecutionContext
import org.springframework.beans.factory.annotation.Autowired

class SomeCommand implements ApplicationCommand {

    @Autowired
    GrailsApplication grailsApplication

    boolean handle(ExecutionContext ctx) {
        // go to town...
        return true
Reply all
Reply to author
Forward
0 new messages