File Templates: Lexical error Encountered "@" (64), after: ""

I get this error message when trying to use a coffeescript file template:

Unable to parse template "Coffeescript jQuery Plugin"
Error message: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 18, column 35.  Encountered: "@" (64), after : ""

Here is my template:

#parse("Coffeescript File Header.coffee")

(($, window) ->

     # Define the plugin class
     class ${NAME}
          ${CODE}

          defaults:
               ${paramA}: ""

          constructor: (el, options) ->
               @options = $.extend({}, @defaults, options)
               @$el = $(el)

          # Additional plugin methods go here
          myMethod: (echo) ->
               @$el.html(@options.${paramA} + ": " + echo)

     # Define the plugin
     $.fn.extend ${NAME}: (option, args...) ->
          @each ->
               $this = $(this)
               data = $this.data("${NAME}")

               if !data
                    $this.data "${NAME}", (data = new ${NAME}(this, option))
               if typeof option == "string"
                    data[option].apply(data, args)) window.jQuery, window

I will appreciate any suggestions on fixing this.

2 comments

Hi there,

I'm not familiar with CoffeScript, at all .. so forgive me is I will say something stupid.

What is $el is?

Is it need to be like that in file after transformation (i.e. it will stay as is) .. or is it meant to be just another variable like ${CODE} or ${paramA} are?

  • If first: you need to escape $ sign by ${DS}, like this: @${DS}el (P.S. same applies to $this)
  • If it supposed to be template variable .. then it needs to be ${el}


For first case (sorry -- tested in PhpStorm with no CoffeScript plugin enabled .. but it works with no errors):

#parse("Coffeescript File Header.coffee")

(($, window) ->

     # Define the plugin class
     class ${NAME}
          ${CODE}

          defaults:
               ${paramA}: ""

          constructor: (el, options) ->
               @options = $.extend({}, @defaults, options)
               @${DS}el = $(el)

          # Additional plugin methods go here
          myMethod: (echo) ->
               @${DS}el.html(@options.${paramA} + ": " + echo)

     # Define the plugin
     $.fn.extend ${NAME}: (option, args...) ->
          @each ->
               ${DS}this = $(this)
               data = ${DS}this.data("${NAME}")

               if !data
                    ${DS}this.data "${NAME}", (data = new ${NAME}(this, option))
               if typeof option == "string"
                    data[option].apply(data, args)) window.jQuery, window

0

$ has a special meaning in velocity... Please see
http://www.jetbrains.com/webstorm/webhelp/file-and-code-templates-2.html:
'You can prevent treating dollar characters ($) in template variables  as prefixes. If you need a dollar character ($ inserted as is, use the  ${DS} file template variable instead. When the template is applied, this  variable evaluates to a plain dollar character ($).
For example, to use some version control keywords (such as $Revision$,  $Date$, etc.) in your default class template, write  ${DS} instead of the  dollar prefix ($). '


So, you have to escape '$' in your template using  ${DS}. like

(($, window) ->

     # Define the plugin class
     class ${NAME}
          ${CODE}

          defaults:
               ${paramA}: ""

          constructor: (el, options) ->
               @options = $.extend({}, @defaults, options)
               @${DS}el = $(el)

          # Additional plugin methods go here
          myMethod: (echo) ->
               @${DS}el.html(@options.${paramA} + ": " + echo)

     # Define the plugin
     $.fn.extend ${NAME}: (option, args...) ->
          @each ->
               ${DS}this = $(this)
               data = ${DS}this.data("${NAME}")

               if !data
                    ${DS}this.data "${NAME}", (data = new ${NAME}(this, option))
               if typeof option == "string"
                    data[option].apply(data, args)) window.jQuery, window

0

Please sign in to leave a comment.