[Acceleo] Can I modify a section of a template from another section? [message #791991] |
Mon, 06 February 2012 13:49 |
Seref Arikan Messages: 73 Registered: August 2010 |
Member |
|
|
Greetings,
I've used stringtemplate before, and I'm used to adding content to fields on a template as a go along. An example would be walking a tree structure based on EMF: as I encounter new types, I'd be adding them to imports section of the file that must be generated.
It appears that with Acceleo, if I have a call to a java service wrapper at the beginning of the template, that call is performed, and that is it.
I want to make sure that I understand the usage of Acceleo correctly. In that case, I'd have to explore the tree structure for imports (possibly through a template that produces imports only), then I'd have to repeat the walk over the tree again, this time for different output. There is no "state" to keep among calls contained in a template.
Is this the correct approach?
Best regards
Seref
|
|
|
Re: [Acceleo] Can I modify a section of a template from another section? [message #793579 is a reply to message #791991] |
Wed, 08 February 2012 10:03 |
|
Hi Seref,
Unfortunately, Acceleo does not provide yet an easy way to manage the "imports" of a generated java file : we do not provide post-processing of any sort on a "file" block, they are generated on-the-fly on disk. You can work around it by making use of the fact that Java services called from Acceleo are stateful (we do not recommand this, but it can serve as a workaround until we introduce the necessary functionnality).
Basically, what you would do is :
- create a "main" template that will hold your "[file]" block.
- create a "generate" template that generates a "placeholder" for the imports and the body of your class while registering the necessary imports in a set/list through a java service
- From your "[file]" block, call the "generate" template as argument of a "replace" operation that replaces the "placeholder" by the content of the import list saved in the java service.
Laurent Goubet
Obeo
|
|
|
|
Re: [Acceleo] Can I modify a section of a template from another section? [message #793705 is a reply to message #793673] |
Wed, 08 February 2012 13:07 |
|
Seref,
I don't remember where ATM, but this shoud be in the user-guide. There are a number of ways you can turn your template. The first is to simply use "replace" with your placeholder string :
[template public main(...)]
[file ...]
[generate(...).replace("<PLACEHOLDER>", getImports(...))/]
[/file]
[/template]
[template public generate(...)]
<PLACEHOLDER>
public class MyClass {
MyClassBody
}
[/template]
[query public getImports(...) : String = invoke(<invoke java service to retrieve import list and convert it to a String>)/]
A second way would be to use post-processing on the template to call the replace :
[template public main(...)]
[file ...]
[generate(...)/]
[/file]
[/template]
[template public generate(...) post (replace("<PLACEHOLDER>", getImports(...)))]
<PLACEHOLDER>
public class MyClass {
MyClassBody
}
[/template]
[query public getImports(...) : String = invoke(<invoke java service to retrieve import list and convert it to a String>)/]
Laurent Goubet
Obeo
|
|
|
|
Powered by
FUDForum. Page generated in 0.03761 seconds