Dollar signs as strings in templates?

I'm trying to store templates in PHPStorm that include the dollar sign ('$') as part of a string.  It looks like the built-in template parser uses the dollar sign as a trigger for variables.  Is there anyway to either change the default variable wrapper character (maybe to something less likely to occur in a web app such as ^ or ~) or escape the dollar signs so the parser doesn't attempt to interprest the dollar sign as the start of a variable declaration?

16 comments
Comment actions Permalink

Hi Brian,

It's either $$ or \$ (don't remember exactly, but I think it is actually \$) to have the $ in the end (after processing template) -- try on some simple template first.

1
Comment actions Permalink

Thanks for the reply.  I tried both of your suggestions, but neither seemed to work.  Maybe there's a config somewhere to allow me to turn off template variables (I don't use them at all...)  

0
Comment actions Permalink

Hi Brian,

(To double check) As I understand we are talking about File Templates (Settings | File Templates), not Live Templates, right?

In any case:

  1. Please provide an example (or two) of your template(s) -- template itself (plain text, so it can be copy-pasted) and screenshot of how does it look after/during processing
  2. State your OS and full PhpStorm version (Help | About)

Maybe there's a config somewhere to allow me to turn off template variables (I don't use them at all...)  

There is no such option available.

0
Comment actions Permalink

To escape a dollar character ($):
1) in Live Templates, use an extra $ (dollar) character.
2) In File Templates, use a backslash (\) in accordance with the Velocity Syntax.

See also http://www.jetbrains.com/phpstorm/webhelp/escaping-characters-in-velocity-variables-and-directives.html
http://www.jetbrains.com/phpstorm/webhelp/creating-and-editing-live-templates.html

1
Comment actions Permalink

Ok, so everything I'm trying based on the suggestions and my own guesses doesn't seem to work.  I am using File Templates (not Live Templates) and I'm using PHPStorm vers 3.0.3 on OSX 10.7.4.

Here's an example of a template for my basic Backbone View object:

define(function(require){

    var $ = require('order!jquery');

    var _ = require('order!underscore');

    var Backbone = require('order!backbone');

    var Mustache = require('order!mustache');

    var bus = require('bus');

    

    var Collection = require('path/to/Collection.js');

    var ChildView = require('path/to/item.view.js');


    var template = require('text!../path/to/template.html');


    var View = Backbone.View.extend({

        tagName: 'div',

        className: 'myViewClassName',


        initialize: function(options){

            this.options = options || {};

        },

        

        render: function(){

            this.options.i18n = function(){

                return function(i18nKey){

                    return $.i18n.prop(i18nKey);

                }

            };

            this.$el.html(Mustache.to_html(template, this.options));


            this.collection = new Collection();


            this.renderItems();


            return this;

        },


        renderItems: function(){

            var self = this;


            this.collection.each(function(model){

                self.$el.append(new ChildView(model.attributes).render().el);

            });

        }

    });


    return View;

});


Whenever I try to create a new file from this template, it hangs on the dollar signs.  It fires an alert modal that tells me the parser was expecting different characters than it ran into:


Unable to parse template "backbone view"

Error message: Encountered "));

            this.collection = new Collection();

            this.renderItems();

            return this;

        },

        renderItems: function(){

            var self = this;

            this.collection.each(function(model){

                self." at MyTemplate[line 27, column 67]

Was expecting one of:

    "[" ...

    "," ...

    ")" ...

     ...


I tried both the '$$' and '\$' escape cues, but neither prevented the alert modal.  As a workaround, I'm storing a special character string for the dollar signs 'DOLLAR_SIGN' that I run a string replace on immediately after the file has been created.  A bit cumbersome, and it works, but I'd really like to be able to just turn off the template parser as it's not something I'm going to use at this point.


Thanks.

0
Comment actions Permalink

Hi Brian,

Thanks for example. I have got the same error (regardless of any escaping I have tried).

For me it looks like this issue (check the last comment, seems similar to yours): http://youtrack.jetbrains.com/issue/WI-8979

If you think it is not exactly your case (which quite possibly can be the case) then submit new ticket as Issue Tracker: http://youtrack.jetbrains.com/issues/WI

0
Comment actions Permalink

Yep, looks like others are having exactly the same issue.  I added to the issue thread.

Thanks!

0
Comment actions Permalink

Thanks for your feedback gents. Looks like platform bug, we'll work on it as soon as possible.

0
Comment actions Permalink

@Ronals Golla

Your case is Live Templates while original post is about File Templates.

 

For Live Templates it's not triple $ but just double $

From your example:

protected $$$attribute$ = '';

$$ -- will represent $ sign in the final text

$attribute$ -- actual variable

http://www.jetbrains.com/help/phpstorm/2016.3/creating-and-editing-live-templates.html

0
Comment actions Permalink

Hi, sorry for wrong topic. but two are not working for me. 2017.1 EAP

0
Comment actions Permalink

What do you mean by "not working" exactly? Can you please clarify?

Based on your example (I hope it's more clear this way):

$$$attribute$ = $$ + $attribute$  (dollar + variable)  -- will be $var in the final text

If that's what you mean by "triple $" -- so be it. But technically it's "double $" + variable (variable name is surrounded by dollar signs)

0
Comment actions Permalink

Just in case somebody stumbles upon this thread like I did. Neither $$varname nor \$varname would work. You should use ${DS}varname instead. DS stands for dollar sign.

3
Comment actions Permalink

SO if I want my Live Template to insert DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" what do I need to put in Live Template?

0
Comment actions Permalink

@Pouledodue

For a Live Template? Just double the $ signs:

DIR="$$( cd "$$( dirname "$${BASH_SOURCE[0]}" )" && pwd )"

 

1

Please sign in to leave a comment.