Code Generation With Grail-s Templates or Custom Scripts

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Code Generation With Grail-s Templates or Custom Scripts

Saleh Mamdouh
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 ?

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/1ad251c9-acd9-4e95-9165-e6f4c6d384c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Jeff Brown
On June 17, 2016 at 11:20:35 AM, Saleh Mamdouh
([hidden email]) 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/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAEfmkrjwHFO7f7b5pFU3Pcg7jVDe6%2BdgyOBfDFJT6gdEz99TTw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Victor Kakama
In reply to this post by Saleh Mamdouh
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.

On Friday, 17 June 2016 19:20:18 UTC+3, Saleh Mamdouh wrote:
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 ?

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/bc5e459e-e884-4a19-8b08-bf4441eecba0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Jeff Brown
On June 20, 2016 at 4:32:36 AM, Victor Kakama ([hidden email]) 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.



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/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAEfmkrhUqRuH0LoBC2BCXo%2BjnXeL1F0-7%3DJH%2Bogj%3Dns2mtHXxQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Jeff Brown


On June 20, 2016 at 7:03:20 AM, Jeff Brown ([hidden email]) wrote:

>  
>  
> On June 20, 2016 at 4:32:36 AM, Victor Kakama ([hidden email]) 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?



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/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/etPan.5767f39d.7f74043f.1d6d%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Victor Kakama
In reply to this post by Saleh Mamdouh
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:
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 ?

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Saleh Mamdouh

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 ?


On 20 June 2016 at 12:21, Victor Kakama <[hidden email]> wrote:
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:
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 ?

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CANzPHmcu-i-BtYcE_oCxYePPDQrq_fK17ZAn5z-_F1sAuNdf5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Code Generation With Grail-s Templates or Custom Scripts

Jeff Brown


On June 21, 2016 at 12:02:58 PM, Saleh Mamdouh ([hidden email]) 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
    }
}




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/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/etPan.57699875.61e2039a.2b8%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.