Search code examples
javascriptvelocitywebstorm

WebStorm velocity template engine


I have trouble creating a JavaScript template in WebStorm, which is using velocity template engine.

The final document I am trying to get looks like this:

app.service('Name', ['$http', function($http) {
 var singularUrlbase = apiurl + 'SingularName';
 var pluralUrlbase = apiurl + 'PluralName';

 this.get = function(id){
   return $http.get( singularUrlBase + id );
 } 
}

I am trying with this template:

#set ( $url1 = 'singularUrlBase + "/" + id')
#set ( $url2 = "singularUrlBase + '/' + s.Id")

app.service('${NAME}', ['$http', function($http) {

    var pluralUrlBase = apiurl + '$pluralName';
    var singularUrlBase = apiurl + '$singularName';

    this.get = function(id) {                        
        return $http.get( $url1 );
    };
}

The problems are:

  • It displays return $http.get ( $url1 ) instead of return $http.get( singularUrlBase + id );
  • I don't know how to ignore $http, WebStorm is asking me to setup $http variable, but I don't want to.

Solution

  • To escape a $ symbol in jetbrains apps templates, you need to write ${DS} as shown in the docs

    template code

    Should look like this:

    #set ( $url1 = 'singularUrlBase + "/" + id')
    #set ( $url2 = 'pluralUrlBase + "/" + id')
    
    app.service('${NAME}', ['${DS}http', function(${DS}http) {
    
        var pluralUrlBase = apiurl + '$pluralName';
        var singularUrlBase = apiurl + '$singularName';
    
        this.get = function(id) {
            if (this.get > 1) {
                return ${DS}http.get( $url1 );
            }
            else {
                return ${DS}http.get( $url2 );
            }
        }
    }]);
    

    inputs

    file name: young_people
    pluralName: child
    singularName: children

    result:

    app.service('young_people', ['$http', function ($http) {
    
        var pluralUrlBase = apiurl + 'child';
        var singularUrlBase = apiurl + 'children';
    
        this.get = function (id) {
            if (this.get > 1) {
                return $http.get(singularUrlBase + "/" + id);
            }
            else {
                return $http.get(pluralUrlBase + "/" + id);
            }
        }
    }]);
    

    Note: your syntax was a bit wrong, I fixed the two last lines ;).