documentcloud / jammit

Industrial Strength Asset Packaging for Rails
http://documentcloud.github.com/jammit/
MIT License
1.16k stars 197 forks source link

Jammit not seeing our template files as templates #62

Closed stephband closed 14 years ago

stephband commented 14 years ago

We've followed all the instructions for making .jst templates but Jammit is not 'seeing' our .jst files as templates, and is instead packaging them as javascript. As a result, the browser is throwing a SyntaxError: Parse error and the JST object has no properties.

Our config/assets.yml has:

template_extension: jst
template_function: on

javascripts:
  templates:
    - app/views/users/card.jst

Yes, the file app/views/users/card.jst exists. We're using bundler, but we forced it to upgrade to 0.5.1 by adding to the /Gemfile:

git 'http://github.com/documentcloud/jammit.git', :tag => "0.5.1" do
    gem 'jammit'
end

We're on Rails 3.

We've tried various combinations of the options template_function, template_namespace, template_extension (I realise we shouldn't need any of them by default). We're able to change the namespace without problem, but the resulting object remains empty.

Any ideas where we're going wrong? Any more information I can give you to help track the problem?

Cheers, Stephen.

jashkenas commented 14 years ago

Hi Stephen.

JavaScript templates are supposed to be packaged as JavaScript. It sounds like you have a syntax error somewhere in your template. Feel free to paste the contents of the template into a gist, and I'd be glad to take a look at the particulars.

stephband commented 14 years ago

Aha. That put me on the right track. So you can't use javascript keywords as variable names in a template. This causes the problem:

  <div class="<%= class %>"></div>

This doesn't:

  <div class="<%= thingy %>"></div>

...because 'class' is a javascript keyword? I note also that stringifying the variable name:

  <div class="<%= 'class' %>"></div>

Doesn't cause an error, but nor does it register 'class' as a variable name. Is it not possible to stringify these variable names while parsing the template, so that any word can be used? I see that the resulting function uses with(obj) to set the scope when generating the template string from its array, so I can see as how that wouldn't work. You would need something like:

  p.push('<div class="', obj[ 'variableName' ] ,'"></div>');

...without the with(obj) statement to make that work. I'm guessing there's a good reason you don't do that? Speed?

Anyway, thanks for the clue, and the fast response, that's a great help.

Cheers, Stephen.

jashkenas commented 14 years ago

Yes, you have to use valid JavaScript variables. It's just arbitrary JavaScript code within the template interpolations...