Search code examples
phpstorm

How can I create PHP templates for PhpStorm with existing code inside?


I want to create a php Template in PhpStorm with has a comment block at the top with the usual info (author, creation date, class etc.) but also with a bunch of premade functions.

The purpose of this is that I want to make PHP Unit Class Template with the setup/teardown functions already coded, because these template are used for one project I don't expect they will change as the setups just set global which really should always be set up to make building the test easier (i.e. getting global scoped helpers).

I've tried creating the file templates however when I've copied the code into the template, any variable comes up as in input box when I go to create the file, which might be fine for me using but for someone who's using it for the first time they can screw up by filling in values for this-.

So I am wondering, how can I create a template in PhpStorm which has code in it?


Solution

  • You need to escape $ character which is used by Velocity template engine internally (has special meaning).

    You can use ${DS} or \$ for that; so $this will become ${DS}this or \$this .

    P.S. ${DS} is a safer choice overall as in some cases \$ may not work.

    The official help page has it all explained: https://www.jetbrains.com/help/phpstorm/file-template-variables.html