Problems with File Templates
Hi, I have been testing the File Templates feature of PyCharm and I've get stocked with the following issues:
1- In the form to define a new File template there is no field to specify a File Name, only Name and Extension fields, nevertheless the all the tutorials mention the existence of a 'File Name' field in that form.
2- How can I apply transformations to the name (${NAME}) the user enters when using my File template.
3- When creating a multi-File template how can I set a directory path relative to the root of the project for every secondary file in the template?
4- What happens if some of those secondary files already exists? Can I introduce some kind of logic in the template to manage that scenario?
Best regards,
Maykell
Please sign in to leave a comment.
Works fine here and it's something that I use very often. I'm not an expert but I got it working fine so I might be able to help you with some of your issues.
1. The new file template lets you put the Name, which is the name shown on the list of templates, then the File Name and Extension as shown above in the Orange rectangle, it seems strange that you don't have it.
2. What kind of transformation? The variable ${NAME} it's not predefined, so when you use it, it should pop a dialogue asking you to insert the value for it, if you look to have the name of the file inserted you need to use ${FILE_NAME} and if you want the name of the user ${USER} but this one will use the username of you actual machine that is currently logged in, on my case, I didn't like it and create a custom one called ${AUTHOR} which is technically doing the same as the one you mention.
3. You first create a template file by clicking the Create template and filling up as required, then, to create more files you need to click "create child template file", you will need to create one for each file you want to add, make sure that when you press Create child, you have the root of your file structure selected to avoid adding it in the wrong template.
If in the file name you type just the name without slashes, it will be created in the root folder of the project and if you need to put it in a specific folder, just write it in the file name dialogue, on the example below, my "web structure" template creates the main html, and then I have several children creating other files, the ones highlighted below is styles/reset, meaning it will create the folder styles and then inside, the file reset, let's say next file needs to be called test and be in a subfolder of styles called demo you can type styles/demo/test and whatever extension needs to be.
4. At default, if your template creates a file that already exists in the project, it will throw an error saying it cannot parse the template and in my experience, it seems to enter a loop, not sure there is a workaround for that.
No, I definitely had that option from the beginning as default, and didn't do anything special to get it, I'm wondering what version of Pycharm you are running, I'm using the Professional version, are you running Pycharm Comunity or Edu? I never used them so I don't personally know their limitations, but I can see you are missing quite a few default templates too. If you look at the list of templates from my screenshot, all the ones in white are default and the ones in blue are custom with the Pro version.
That was the problem, the IDE version. I was using an old version of Pycharm Community.
I made an update of the IDE and the field shows up.
Thank very much Farigiovanni !!