How to execute functions in file templates?

I simply want to camelcase the filename. I can't find any ressources on this.

Here's what I tried:

 

#set( $name = camelCase($NAME) )

$name

 

#set( $name = $camelCase($NAME) )

$name

 

#set( $name = $camelCase$NAME )

$name

 

#set( $name = camelCase$NAME )

$name

 

None of these works.

 

3 comments
Comment actions Permalink

Hi there,

#set($class = ${NAME})
#set($class = $class.toUpperCase())

 

Few more examples (code is for PHP, but that should make no difference for you):

#set($class = ${NAME})


#set($class = $class.substring(0).toLowerCase())
class Controller1_${class}
{}

#set($class = $class.toUpperCase())
class Controller2_${class}
{}

#set($class_start = $class.substring(0,1).toUpperCase())
#set($class_rest = $class.substring(1).toLowerCase())
class Controller3_${class_start}${class_rest}
{}
2
Comment actions Permalink

File Templates use Apache Velocity template language (http://velocity.apache.org/). You can try using Apache Velocity StringUtils methods in your file templates. For example:

/**
 * File_name to FileName: ${StringUtils.removeAndHump($NAME)}. 
 */

Standard java String functions can also be used - see http://stackoverflow.com/questions/6998412/velocity-string-function, for example.

2
Comment actions Permalink

Another example:

## will convert foo_BAR_meow into FooBarMeow

#set($class = ${NAME})
#set($parts = $class.split("_"))
#set($final = "")
#set($current = "")
#foreach($current in $parts)
#set($fc = $current.substring(0,1).toUpperCase())
#set($oc = $current.substring(1).toLowerCase())
#set($final = "$final$fc$oc")
#end
class Controller_${final}
{
}

I guess it might be done better now (some better/standard Java function may exist), I wrote this back in 2012 or so (8 years ago)

0

Please sign in to leave a comment.