El Mac
El Mac

Reputation: 3419

IntelliJ Custom Template Variable

I am trying to create my custom template with a custom variable called $MyName. And I think it should be possible for IntelliJ to ask about the variable's value if it was not initialized. In the documentation they provide some informations about how to do this, but they don't have a real example. They just say, it somehow IntelliJ Idea will ask me to specify it.

If, when applying a template, the values of certain template variable are not known, IntelliJ IDEA will ask you to specify them.

How can I create a custom variable like $MyName and then when the user uses the template, it will ask him what his name is?

IntelliJ IDEA 2016.1 Documentation

Edit (19.04.2016): Added Screenshots

Screenshot Settings Window

As you can see, the custom variable is not recognized or not valid. If I declare it like ${MyName}, it won't work either.

New File Screenshot

Upvotes: 2

Views: 2127

Answers (2)

maojunxyz
maojunxyz

Reputation: 27

File Template Variables

File | Settings | Editor | File and Code Templates
You should set it in files. If set in includes, the new file is directly output variable name instead of variable content. For example, set the Java Class file Custom variable$DESCRIPTION.If the value of a variable is not defined in the template, IntelliJ IDEA will ask you to specify it when the template is applied.
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") /** * Description: $DESCRIPTION * Created by ${USER} * Date: ${YEAR}/${MONTH}/${DAY} * Time: ${TIME} */ public class ${NAME} { }

you can also define the values of custom variables right in the template using the #set directive. For example, if you want to use your full name instead of your login name defined through the predefined variable ${USER}, use the following construct:
#set( $MyName = "John Smith" )

Reference link

Upvotes: 0

Peter Gromov
Peter Gromov

Reputation: 18931

It's a bug in IntelliJ IDEA, filed as https://youtrack.jetbrains.com/issue/IDEA-154958. Thanks for noticing!

Upvotes: 2

Related Questions