8

I am trying to use this section of code from jQuery UI's tabs example and convert it to Coffeescript. I've run it through the awesome http://js2coffee.org/ tool.

var tabTitle = $( "#tab_title" ),
    tabContent = $( "#tab_content" ),
    tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
    tabCounter = 2;

The problem is that tabTemplate variable declaration. Coffeescript is trying to do string interpolation on it, as far as I can tell. I've tried escaping it with a slash, but that just resolves to using a slash in the converted js.

2 Answers 2

15

Use single-quotes to delimit your string: http://coffeescript.org/#strings

If you want to use single-quotes within your string without manually escaping them you can use 3 single-quotes:

x = '''
my string's ok with single quotes and #{doesn't interpolate}
'''

That said, this is HTML, so double-quotes are actually more common for attributes than single-quotes. Your string could therefore be written as:

tabTemplate = '<li><a href="#{href}">#{label}</a> <span class="ui-icon ui-icon-close">Remove Tab</span></li>'

without any problems.

Sign up to request clarification or add additional context in comments.

7 Comments

Yuck. Is that the best we can do? Why don't the double-quotes work?
Because double-quoted strings are what cause the interpolation :) That's the difference between double- and single-quoted strings. See coffeescript.org/#strings
Still yuck. Is there any way to escape the entire string, like C# does with @"some\string"?
Yep, that's what the triple-single-quotes do :)
Oh. It's not at all clear from the CoffeeScript documentation that triple-single quotes (mind boggles) is the way this works. Anyway, I upvoted your answer based on your second idea.
|
8

Escaping with backslash does work:

$ coffee -bce '"\#{a}"'
"\#{a}";

$ coffee -bce '"#\{a}"'
"#\{a}";

1 Comment

So, we can do: str = " \"#{text wrapped with double quotes}\""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.