How to work with templates

Hi ! I'm trying to add some templates (and then use them) in my plugin, but I couldn't make it work.
I'm implementing FileTemplateGroupDescriptorFactory and registering the extension point, it is executed and it's adding some new entries on Settings > File Templates.
But I don't know how to fill those templates, should I create an *.ft file?

Please a bit lost with this, can you explain me better how it works and what should I do?

Thanks !!
Enrique.

4 comments
Comment actions Permalink

OK, this is what I have so far:

I have created a file called "Test.php.ft" inside "fileTemplates" package (not  fileTemplates.j2ee), and I've added ";?*.ft" to the  compiler options, the template is now added automatically (and with content)  to the "Templates" tab, even more, I don't need the extension point for this.

And I can get the content of the template using:



Now, If I put the file inside fileTemplates.j2ee package and use the extension point:

    public FileTemplateGroupDescriptor getFileTemplatesDescriptor() {
        String title = "My Title";
        final FileTemplateGroupDescriptor group = new FileTemplateGroupDescriptor(title, myIcon);

        group.addTemplate(new FileTemplateDescriptor("Test.php", myIcon));

        return group;
    }


Then a new tab called Java EE (I'm using PHPStorm) is created in Settings > File Templates, inside that tab I have a group called "My Title" with my icon, inside that group I can see "Test.php" with my icon, and the content is loaded correctly too.

The problem is that I don't know how to get that template, I get null if I try to do:

FileTemplateManager.getInstance().getTemplate("Test.php");


Can you please tell me how can I change the title for the Java EE tab (that is, create a custom tab), and also how can I get the templates from the new tab?

Thanks !

0
Comment actions Permalink

Hi Enrique,

As you correctly figured out, your .ft file should be placed under the "fileTemplates" directory somewhere in your plugin's classpath.
This is so colled "default" template category.
There are also several predefined template categories:

"internal", "includes", "code", "j2ee";

So if you want to place your template under one of these categories, just put the .ft file under
fileTemplates/internal, fileTemplates/includes etc.

For obtaining a template depending on its category use corresponding methods:

FileTempleteManager.getTemplate()
FileTempleteManager.getInternalTemplate()
FileTempleteManager.getPattern()
FileTempleteManager.getCodeTemplate()
FileTempleteManager.getJ2eeTemplate()


Regards,
  Eugene Zhuravlev
0
Comment actions Permalink

Thanks Eugen, so I can't create my custom tab with my custom group and icons?
If I use

final FileTemplateGroupDescriptor group = new FileTemplateGroupDescriptor(title, myIcon);
 
group.addTemplate(new FileTemplateDescriptor("Test.php", myIcon));
 


Then it creates e agroup with my icons, but inside the tab Java EE tab.
And If I don't use anything and just put the files inside "fileTemplates" it adds the templates automatically, but with the default icon according to the extension and it also shows the templates when user uses Alt+Insert.

0
Comment actions Permalink

> so I can't create my custom tab with my custom group and icons?

You can't create a custom tab, but you can create a custom group in a way you mentioned.
The group will be displayed under the J2EE tab. The name of the tab is legacy and indeed counterintuitive. I think we'll change it. Thanks for pointing on this.

Eugene.

0

Please sign in to leave a comment.