2

I want to create my own file templates. However something is wrong with variables, since I am prompted for such variables as namespace. Quick example:

<?php
namespace ${NAMESPACE};

class ${NAME} 
{

}

This prompts me for both namespace and name, while I should only provide a file name. How can I make PHPStorm to auto-detect its dedicated variables?

I am using 7.1.3 version on latest OSX.

1 Answer 1

10

How can I make PHPStorm to auto-detect its dedicated variables?

You cannot.

${NAMESPACE} is pre-defined/pre-filled only in bundled templates where PHP plugin provides special support for it. In all other cases it is treated as ordinary variable.

http://youtrack.jetbrains.com/issue/WI-21711


UPDATE: 2020-01-28

The aforementioned ticket is now resolved.

You can now select custom template when using "New PHP Class/Trait/Interface" dialog. ${NAMESPACE} variable now gets taken from that dialog field (where it auto-detected based on your Source Roots settings and you can edit it manually if needed) and passed into the actual template.

Sign up to request clarification or add additional context in comments.

10 Comments

That's sad. Makes creating own templates rather useless. Thanks for a quick answer.
Well ... you can edit bundled template. And with installed File Template Variables plugin you can make it quite customisable on per project level. Not ideal and I do not know how different your templates should be .. but something to look into. (P.S. Templates support if-else logic ...)
@LazyOne I know how existing $NAMESPACE variable works, you don't have to tell me that. I can see myself :) What I want is to know if it is possible to have namespace automatically added and not type it manually. As it appears, this very much needed functionality is not implemented and so many users are not taking advantage of custom file templates. The ticket is open for more than 2 years and nothing has been done about it. What can we do? Did you post your vote/comment already there? If not - just do it. Now. Thanks.
You can now select a template when creating new PHP Class which solves the problem.
@ArielBogdziewicz This works only when "New Class" DIALOG WINDOW is appearing; because it's that window that provides such extra variables (for that specific case). For your case (multi-file template) it will NOT work as that dialog window is NOT involved. P.S. Will it create multiple files if you use that New Class and select your template there? I think it will work for the main file and not for the child ones; still worth checking if you have not done yet.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.