Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a template within a template - eclipse

Tags:

java

eclipse

As I slowly crawl in my transition from .net to Java, I find more and more interesting things about the eclipse IDE. I recently stumbled upon its templates and I'm loving it. Which brings me to a question: can I call a template from within a template? Of course it would be merely a copy and paste matter, but I'm wondering if it can be done.

like image 865
makoshichi Avatar asked May 04 '11 13:05

makoshichi


1 Answers

Yes, actually, you can and there is an example right in the default set.

If you go to your Preferences -> Java -> Code Style -> Code Templates, you can Export All of the provided Java templates. In there you will see the following File template (formatted for readability):

<template 
    autoinsert="true" 
    context="filecomment_context" 
    deleted="false" 
    description="Comment for created Java files" 
    enabled="true" 
    id="org.eclipse.jdt.ui.text.codetemplates.filecomment" 
    name="filecomment">
    /** * */
</template>

and a bit further down, the New Type which makes use of that File template:

<template 
    autoinsert="true" 
    context="newtype_context" 
    deleted="false" 
    description="Newly created files" 
    enabled="true" 
    id="org.eclipse.jdt.ui.text.codetemplates.newtype" 
    name="newtype">
    ${filecomment} ${package_declaration} ${typecomment} ${type_declaration}
</template> 

So, if you'd like to make a template use another, the basic form is to refer to the id of your sub-template with the dollar sign prefix. For example:

<template 
    autoinsert="true" 
    context="BobOuter_context" 
    deleted="false" 
    description="Bob example outer template" 
    enabled="true" 
    id="bob.example.outertemplate" 
    name="BobOuter">
    BobOuterBegins Insert inner template ${bob.example.innertemplate} BobOuterEnds
</template> 

<template 
    autoinsert="true" 
    context="BobInner_context" 
    deleted="false" 
    description="Bob example inner template" 
    enabled="true" 
    id="bob.example.innertemplate" 
    name="BobInner">
    BobInnerBegins Super awesome content goes here BobInnerEnds
</template> 
like image 83
Bob Cross Avatar answered Oct 24 '22 04:10

Bob Cross