PHPStorm File Templates Includes Custom Variables

PHPStorm’s File template settings are very versatile. They allow you to not only make a file template, but also allow you to parse out redundant elements (such as licensing) in the template by allowing an includes section. In your File Template, any undefined variable automatically elicits a prompt from PHPStorm.

PHPStorm allows you access to the includes section via the #parse directive. If you’re want To have Custom variables to be filled in correctly via prompt, you will need to have the variable declared in the template.

Example

“chance license.php”

/**
* @package ${Package}
* @author Chance Garcia
* @copyright (C)Copyright ${YEAR} chancegarcia.com
*/

In the above includes example, I’m wanting to have a custom variable named Package. I can only cause PHPStorm to prompt for this value if I include the variable in my template. If I’m already using the variable in the template, then it will fill in when the includes file is parsed.

Example:

<?php
#parse("chance license.php")

class ${Package}_#if(${ExtraClassInfo} != "")${ExtraClassInfo}_#end${NAME}
{

}

In the above template, the ${Package} variable will be given a prompt since it is used in the template and an unknown variable and the parsed “chance license.php” include will be able to use that prompt value.

I am also using another variable to Prompt for extra class name information. Since PHPStorm uses Velocity Template Language (VTL), I am able to use the VTL conditional syntax to insert that information if it is entered and ignore it if it is not. This technique is useful in a situation where you want your include file to have a custom variable value but do not need to display this value in your template.

Example:

<?xml version="1.0"?>
<!--
#if(${Package})#end
#parse("chance license.php")
-->

 

In the above example, we make PHPStorm prompt for the custom value needed for out parsed include file. This gives us our expected include file without printing our custom variable anywhere else in our template.

 

Update: I made a github repo (https://github.com/chancegarcia/phpstorm-templates) of my templates for better examples and in case I lose my settings.

Enhanced by Zemanta

1 comment on “PHPStorm File Templates Includes Custom VariablesAdd yours →

Leave a Reply