User input at file template creation

已回答

Hi.

I'm trying to create a clever DTO template, i.e. mainly a class that holds a number av values accessible through getters.

I know I can write $-variables in the template which will be represented by fields in my template creation popup so that I can name all my values in one place.
Like this:
templatePopup.png

I can also make so that if a value isn't entered in a "var.."-box, the value won't show up int the class.


But is there a way that would enable me to choose nr of values to enter in the template creation popup, so that I'm not limited to a fixed nr of values in my template?

0
Avatar
Permanently deleted user

Not directly. But since the file template uses the velocity template language (http://velocity.apache.org/engine/releases/velocity-1.6.4/user-guide.html) you can use some logic to accomplish your goal. Instead of having a separate text field for the number of values desired, you would enter them as a list (delimited as desired).  The following example will take a list of space delimited names and values to create constants.

#set($contantNamesList = $Constant_Names_-_space_delimited_list.split(" "))
#set($contantValuesList = $Constant_Values_-_space_delimited_list.split(" "))


package ${PACKAGE_NAME};
#parse("File Header.java")
public class ${NAME}
{
#set($i=0)
#foreach( ${zz_template_variable_placeholder} in $contantNamesList)
    public static final String $contantNamesList.get($i) = "$contantValuesList.get($i)";
    #set($i = $i + 1)
#end
}


The unfortunate shortcoming is that the IDEA dialog will show the the loop variable. While variables set via the #set directive are not shown, $item in #foreach( $item in $myList) does show in the dialog. So the hackish workaround is to name loop variables in a way that they show at the end of the list (such as starting with a 'z') and perhaps use a name that indicates that it does not need to be set. Thus the use of "zz_template_variable_placeholder" in the above example. You can vote for http://youtrack.jetbrains.com/issue/IDEA-60112 which asks for this shortcoming to be fixed.

The above example shows a dialog as such:
screenshot.png
which creates the code:

public class Example
{
    public static final String ONE = "A";
    public static final String TWO = "B";
    public static final String THREE = "C";
}

1
Avatar
Permanently deleted user

Thanks a lot! I'm really amazed by the quick and extensive repsonses in this community.

I've voted and will stick to my version until it is fixed.

0
Avatar
Permanently deleted user

This helped me a lot. Ive learned much from it. I even found a solution for the problem with an extra field shown that is not needed. To use a variable in a for loop, but you don't want a input field for it, you can just do something like this (Based on the example of Mark Vedder):

 

#set($contantNamesList = $Constant_Names_-_space_delimited_list.split(" "))
#set($contantValuesList = $Constant_Values_-_space_delimited_list.split(" "))
#set($foreachVar = "")

package ${PACKAGE_NAME};
#parse("File Header.java")
public class ${NAME}
{
#set($i=0)
#foreach( $foreachVar in $contantNamesList)
    public static final String $contantNamesList.get($i) = "$contantValuesList.get($i)";
    #set($i = $i + 1)
#end
}

Then you won't have a inputfield for $foreachVar and you can use it as the "key" for the foreach loop.

 

I needed the same but for an xml file. I work with Magento and wanted a template file to add a module.xml file. This is what I did.

#set($SEQUENSES = $Sequenses.split(" "))
#set($i = 0)
#set($sequence = "")

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="${Vendor}_${Module}" setup_version="0.0.1">
        <sequence>
        #foreach($sequence in $SEQUENSES)
            <module name="$SEQUENSES.get($i)"/>
            #set($i = $i + 1)
        #end
        </sequence>
    </module>
</config>

Now I have a dialog that looks like this:

And my file looks like this after creation:

0

请先登录再写评论。