GithubHelp home page GithubHelp logo

handlebars_assets's Introduction

Use handlebars.js templates with the asset pipeline and/or sprockets

Are your handlebars.js templates littering your Rails views with script tags? Wondering why the nifty Rails 3.1 asset pipeline streamlines all your JavaScript except for your Handlebars templates? Wouldn't it be nice to have your Handlebars templates compiled, compressed, and cached like your other JavaScript?

Yea, I think so too. That is why I wrote handlebars_assets. Give your Handlebars templates their own files (including partials) and have them compiled, compressed, and cached as part of the Rails 3.1 asset pipeline!

Using sprockets with Sinatra or another framework? handlebars_assets works outside of Rails too (as of v0.2.0)

BREAKING CHANGES AS OF v0.17

@AlexRiedler has made some larger changes to this repository for going forward; If you have existing monkey patches they may not work, and the configuration schema has changed slightly to handle multiple extensions for the same compilation pipeline.

If you have any problems, please post an issue! I will attempt to fix ASAP

BREAKING CHANGE AS OF v0.9.0

My pull request to allow / in partials was pulled into Handlebars. The hack that converted partial names to underscored paths (shared/_time -> _shared_time) is no longer necessary and has been removed. You should change all the partial references in your app when upgrading from a version prior to v0.9.0.

Version of handlebars.js

handlebars_assets is packaged with the current stable release of handlebars.js. See the section on using another version if that does not work for you.

Installation

Rails 4.0+

Load handlebars_assets in your Gemfile

gem 'handlebars_assets'

Then follow Javascript Setup

Side Note: As of Rails 4.0, the assets group is not supported in the Gemfile (source).

Rails 3.1+

Load handlebars_assets in your Gemfile as part of the assets group

group :assets do
  gem 'handlebars_assets'
end

Then follow Javascript Setup

Sprockets (Non-Rails)

handlebars_assets can work with earlier versions of Rails or other frameworks like Sinatra.

Load handlebars_assets in your Gemfile

gem 'handlebars_assets'

Add the HandlebarsAssets.path to your Sprockets::Environment instance. This lets Sprockets know where the Handlebars JavaScript files are and is required for the next steps to work.

env = Sprockets::Environment.new

require 'handlebars_assets'
env.append_path HandlebarsAssets.path

Javascript Setup

Require handlebars.runtime.js in your JavaScript manifest (i.e. application.js)

//= require handlebars.runtime

If you need to compile your JavaScript templates in the browser as well, you should instead require handlebars.js (which is significantly larger)

//= require handlebars

Templates directory

Generally you want to locate your template with your other assets, for example app/assets/javascripts/templates. In your JavaScript manifest file, use require_tree to pull in the templates

app/assets/javascripts/application.js

//= require_tree ./templates

This must be done before //= require_tree . otherwise all your templates will not have the intended prefix; and after your inclusion of handlebars/handlebars runtime.

Rails Asset Precompiling

handlebars_assets also works when you are precompiling your assets.

rake assets:precompile

If you are using rake assets:precompile, you have to re-run the rake command to rebuild any changed templates. See the Rails guide for more details.

Heroku & other cloud hosts

If you are deploying to Heroku, be sure to read the Rails guide and in your config/application.rb set:

config.assets.initialize_on_precompile = false

This avoids running your initializers when compiling assets (see the guide for why you would want that).

However, that does mean that you cannot set your configuration in an initializer. This issue has a workaround, or you can set:

config.assets.initialize_on_precompile = true

This will run all your initializers before precompiling assets.

Usage

The template files

Write your Handlebars templates as standalone files in your templates directory. Organize the templates similarly to Rails views.

For example, if you have new, edit, and show templates for a Contact model

templates/
  contacts/
    new.hbs
    edit.hbs
    show.hbs

Your file extensions tell the asset pipeline how to process the file. Use .hbs to compile the template with Handlebars.

If your file is templates/contacts/new.hbs, the asset pipeline will generate JavaScript code

  1. Compile the Handlebars template to JavaScript code
  2. Add the template code to the HandlebarsTemplates global under the name contacts/new

You can then invoke the resulting template in your application's JavaScript

NOTE: There will be no javascript object HandlebarsTemplates unless at least ONE template is included.

HandlebarsTemplates['contacts/new'](context);

Partials

If you begin the name of the template with an underscore, it will be recognized as a partial. You can invoke partials inside a template using the Handlebars partial syntax:

Invoke a {{> path/to/_partial }}

Configuration

The template namespace

By default, the global JavaScript object that holds the compiled templates is HandlebarsTemplates, but it can be easily renamed. Another common template namespace is JST. Just change the template_namespace configuration option when you initialize your application.

HandlebarsAssets::Config.template_namespace = 'JST'

Ember Support

To compile your templates for use with Ember.js simply turn on the config option:

HandlebarsAssets::Config.ember = true

If you need to compile templates for Ember and another framework then enable multiple frameworks:

HandlebarsAssets::Config.multiple_frameworks = true

After mutliple_frameworks has been enabled templates with the .ember.hbs extension will be made available to Ember.

.hamlbars and .slimbars Support

If you name your templates with the extension .hamlbars, you can use Haml syntax for your markup! Use HandlebarsAssets::Config.haml_options to pass custom options to the Haml rendering engine.

For example, if you have a file widget.hamlbars that looks like this:

%h1 {{title}}
%p {{body}}

The Haml will be pre-processed so that the Handlebars template is basically this:

<h1> {{title}} </h1>
<p> {{body}} </p>

The same applies to .slimbars and the Slim gem. Use HandlebarsAssets::Config.slim_options to pass custom options to the Slim rendering engine.

Note: To use the hb handlebars helper with Haml, you'll also need to include the Hamlbars gem in your Gemfile:

  gem 'hamlbars', '~> 2.0'

This will then allow you to do things like Haml blocks:

%ul.authors
= hb 'each authors' do
  %li<
    = succeed ',' do
      = hb 'lastName'
    = hb 'firstName'

Reference hamlbars for more information.

Using another version of handlebars.js

Occasionally you might need to use a version of handlebars.js other than the included version. You can set the compiler_path and compiler options to use a custom version of handlebars.js.

HandlebarsAssets::Config.compiler = 'my_handlebars.js' # Change the name of the compiler file
HandlebarsAssets::Config.compiler_path = Rails.root.join('app/assets/javascripts') # Change the location of the compiler file

Patching handlebars.js

If you need specific customizations to the handlebars.js compiler, you can use patch the compiler with your own JavaScript patches.

The patch file(s) are concatenated with the handlebars.js file before compiling. Take a look at the test for details.

HandlebarsAssets::Config.patch_files = 'my_patch.js'
HandlebarsAssets::Config.patch_path = Rails.root.join('app/assets/javascripts') # Defaults to `Config.compiler_path`

Thanks

This gem is standing on the shoulders of giants.

Thank you Yehuda Katz (@wycats) for handlebars.js and lots of other code I use every day.

Thank you Charles Lowell (@cowboyd) for therubyracer and handlebars.rb.

Maintainer

This gem is maintained by Alex Riedler Github.

Author

Les Hill, follow me on Github and Twitter.

Contributors

  • Matt Burke (@spraints) : execjs support
  •                (@kendagriff)     : 1.8.7 compatibility
    
  • Thorben Schröder (@walski) : 3.1 asset group for precompile
  • Erwan Barrier (@erwanb) : Support for plain sprockets
  • Brendan Loudermilk (@bloudermilk) : HandlebarsAssets.path
  • Dan Evans (@danevans) : Rails 2 support
  • Ben Woosley (@empact) : Update to handlebars.js 1.0.0.beta.6
  •                (@cw-moshe)       : Remove 'templates/' from names
    
  • Spike Brehm (@spikebrehm) : Config.template_namespace option
  • Ken Mayer (@kmayer) : Quick fix for template_namespace option
  • Brad Murray (@wyaeld) : Generic options support
  • Blake Williams (@BlakeWilliams) : .handlebars extension
  • Tristan Koch (@trkoch) : Strip leading whitespace from compiled templates
  • Brian Cardarella (@bcardarella) : Ember support
  • David Lee (@davidlee) : Slim support
  • Phil Cohen (@phlipper) : README cleanup
  • Akshay Rawat (@akshayrawat) : Update to handlebars.js 1.0.0-rc.3
  • Turadg Aleahmad (@turadg) : Update to handlebars 1.0.0-rc.4
  • Mark Rushakoff (@mark-rushakoff) : Synchronize Sprockets engine registers with Rails
  • Alex Riedler (@AlexRiedler) : Pass scope and locals up the chain
  • lee (@lee) : Update to handlebars 1.0.0
  • Ken Collins (@metaskills) : Register with Sprockets
  • Blake Hitchcock (@rbhitchcock) : Support ember and other handlebars use simultaneously
  • ajk (@Darep) : Fix .hbs extension for multi-framework support
  •                (@mattmenefee)    : README section for `hamlbars`
    
  • Parker Selbert (@sorentwo) : README section for multiple_frameworks
  • Francisco QV (@panchoqv) : README clarification
  • Dylan Markow (@dmarkow) : README cleanup
  • Peter Boling (@pboling) : AMD Loading

Contributing

Pull requests are welcome! Please do not update the version number.

In a nutshell:

  1. Fork
  2. Create a topic branch - git checkout -b my_branch
  3. Push to your branch - git push origin my_branch
  4. Create a Pull Request from your branch
  5. That's it!

handlebars_assets's People

Contributors

alexriedler avatar cw-moshe avatar darep avatar dgraham avatar disbelief avatar jhawthorn avatar jwlms avatar kmayer avatar langalex avatar lee avatar leshill avatar mark-rushakoff avatar mattmenefee avatar metaskills avatar mobilutz avatar more-ron avatar neodude avatar nickpith avatar omarskalli avatar phlipper avatar rbhitchcock avatar rounders avatar sorentwo avatar spikebrehm avatar spraints avatar srinandanpai avatar tjgrathwell avatar trkoch avatar turadg avatar walski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

handlebars_assets's Issues

Not working on Heroku with Rails 3.1

I'm running this successfully on local, but when pushing to Heroku, I'm getting:

couldn't find file 'handlebars.runtime'

If I add the handlebars.runtime in my libaries, it says that HandlebarsTemplates is undefined. So it seems like the gem is not working at all.

Any tips for fixes? Running Rails 3.1.

handlebars_assets with backbone AND ember...

We have a web site that has several "mini apps" that currently use backbone over rails, leveraging handlebars_assets.

We're wanting to add to this app (might not be an ideal solution) so that our new "mini apps" work with ember instead of backbone.

I'm having problems getting handlebars_assets to play nice in that environment. On one hand, I can get the backbone app to work as it has in the past.

To get ember to work properly, I need an initializer for Handlebars that's configured to help ember, but that changes how the backbone templates are loaded, thereby breaking them.

Is there a way to set how handlebars_assets compiles templates so that both client front ends are happy?

Thanks for any ideas...

Handlebars error rendering Backbone models

When using the default access method for Backbone I get a Handlebars parsing error. The following is the output from Rails.

Error: Parse error on line 13:
...    <li {{#if this.get('language')}}cla
----------------------^
Expecting 'ID', got 'undefined'

This is inside an #each helper where the object being iterated on is a standard array of Backbone models (array generated from underscore filter method).

Using this.attributes.language outputs the correct value. Doesn't seem like the best way for this to work.

broken in Rails 3.2.6?

Upgrading from Rails 3.2.3 to 3.2.6 seems to cause the following error when loading pages in production mode:

Sprockets::FileNotFound: couldn't find file 'handlebars.runtime'

With the following line in our top-level JS file:

//= require handlebars.runtime

Works fine on development, using Sprockets v2.1.3 and handlebars_assets v0.4.4. I wonder if something about the Rails asset search path changed... anyone else seeing this?

Handlebars Version problem

I'm getting a new error:

Uncaught Template was precompiled with an older version of Handlebars than the current runtime. Please update your precompiler to a newer version (>= 1.0.0-rc.3) or downgrade your runtime to an older version (<= 1.0.rc.2).

I tried changing the version to <= 1.0.rc.2 which got rid of the error but returned an empty string.

Any help would be appreciated, thanks.

.rvmrc shouldn't be checked in

Hey Les,

Any chance I could convince you to take the .rvmrc out of the repo? Seems like a per-environment kinda file.

-B

How to use asset pipeline assets from within slimbars templates?

I have a slimbars template like so:

li
  a.profile-button
    img src="/assets/profile.png"
    span.menu-link My Profile

This loads the asset from my rails server. I am deployed on Heroku, and push all my assets to S3, so the assets are available, and fingerprinted, on S3 by the asset pipeline, but I can't figure out how to use asset pipeline'd assets from within my templates.

This is my main template:

        ul.dropdown
          | {{> _menu_list_items }}

In the partial template I render image tags which I would like to server from S3.
I tried adding a .erb on the end of the template and inserting it that way, but resulted in the template being inserted into my view as a giant slimbars string. It was processed by erb, but that prevented the processing by slim.

Here is the partial:

li
  a.profile-button
    img src="<%= asset_path('/assets/profile.png') %>"
    span.menu-link My Profile

became the following in the final DOM (local, so not hitting S3, but it did evaluate the asset_path:

<ul class="dropdown">li
  a.profile-button
    img src="/assets/profile.png"
    span.menu-link My Profile
</ul>

Hoping I missed something obvious, because it seems like this would be a common need, yet I haven't been able to find anything by googling.

README should specify how to add assets path outside of Rails

Hey guys,

Firstly, thanks for the time you've invested in this gem. It really simplified the process of getting Handlebars in a my Sinatra app.

I had one problem, though, and that was informing my Sprockets::Environment instance of the asset path supplied by this gem. I eventually got it working after delving into the source code, but it's quite a hack:

handlebars_path = HandlebarsAssets::Handlebars.send(:assets_path).join("javascripts")
environment.append_path(handlebars_path)

How did you guys intend for the asset path to be included? Let me know and I'll be happy to update the README to include instructions and send a pull request.

Cannot use Handlebars helpers on element tags

Not sure if this is a bug, or a limitation of how Hamlbars and Slimbars are compiled

My understanding is that file extensions .hamlbars and .slimbars first go through the HAML and SLIM templating engine, then that generated HTML w/ Handlebars markup is passed to Handlebars for precompilation.

The issue is this:

The Handlebars helpers don't work on a Haml element tag. Probably because the if logic isn't run until the template is compiled by handlebars. Since HAML compilation happens first it breaks.

Not sure on a fix.

uncaught Error: Haml::SyntaxError: Illegal nesting: nesting within plain text is illegal.

Hamlbars template

{{#if true}} 
.notification-message
{{else}}
.notification-message.message-read
{{/if}}  
  .header 
    .title
      {{title}}
    .close
      x
  .content
    {{content}}
    %span 
      2 Days Ago
  .details 
    44 Hosts Found

hbs not getting removed from compiled asset's filename

Using 0.6.6, for some reason my templates like template.jst.hbs are getting precompiled to template.hbs.js. I'm actually able to access template.js so it's getting precompiled correctly, it's just the filename is wrong. So somehow, hbs is not getting removed from filename. Anyone know what's going on?

What's interesting is that simply template.hbs will become template.js properly. Does that mean it's likely a sprockets issue?

Change the global templates object from `HandlebarsTemplates`?

Hi there,

I see a number of references here to JST[template_name], however in master the templates live in HandlebarsTemplates. I tried to look through the repo history for this change, but I couldn't find it.

Could you add a configuration option to choose the name of the template namespace? We're trying to share some JS code between a Rails 3.0 app running Jammit and a Rails 3.2 app with Sprockets, and it would be nice if they could both reference JST.

You want me to submit a pull request?

HandlebarsTemplates is not defined

Trying to get this gem working in our Rails 3 app, but the global namespace for templates is coming back as 'undefined,' both in my coffee files and in my Chrome console.

My application.js.coffee looks like this:

#= require './lib/jquery.min'
#= require './lib/underscore.min'
#= require './lib/backbone.min'
#= require './lib/marionette.min'
#= require 'handlebars'
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require 'connectors.app'

I've tried moving the ./templates directory up and down the list, and nothing fixes it. Everything else functions just fine (I'm able to call Backbone.Model, for instance).

This is in my dev environment. My development.rb looks like this:

Connectors::Application.configure do
  config.cache_classes = false
  config.whiny_nils = true
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.action_dispatch.best_standards_support = :builtin
  config.active_record.mass_assignment_sanitizer = :strict
  config.active_record.auto_explain_threshold_in_seconds = 0.5
  config.assets.compress = false
  config.assets.debug = true
end

Thanks for any help!

undefined method `to_json' for

Getting this error:
undefined method `to_json' for #String:0x000001015e2188

After setting HandlebarsAssets::Config.ember = true.

in rvm/gems/ruby-1.9.3-p194/bundler/gems/handlebars_assets-7ac9c4b12a26/lib/handlebars_assets/tilt_handlebars.rb:32:in `evaluate'

Adding helpers

I'm sure this is something simple but I haven't been able to figure this out quickly so it might be worth a mention in the README or docs somewhere.

How does one register a helper to be used with templates?

Thanks!

Templates don't get added to Template Namespace

Using the following require is the only way to get the templates added to the global namespace automatically.

//= require_tree ./templates

HandlebarsTemplates'contacts/new';


What if we don't want to include all the templates in our application? An example is a Rails site that may have multiple backbone apps. We don't always want to pull in all the templates.

If we specify

//= require ./templates/foo/bar

then HandlebarsTemplates'foo/bar'; should work, but it doesn't because for some reason HandlebarsTemplates becomes undefined.

Can't access data of nested records in the template. ???

I have justs upgraded an application to Rails 3.2.6 (previous was in 3.2.3) and I have begun to have weird problems with
my handlebars templates.
The problem is that it seems not to get data that come from nested records in the json object.
I have templates lake this:

<div data-role="content">
    <h3 data-role="header">{{ customerName }}</h3>
    <ul id="contacts" data-role="listview" data-inset="true">
    {{#contacts}}
    <li>
        <h3>{{ name }}</h3>
        <p>{{ job }}</p>
        <p>{{ phone}} {{ mobile }}</p>
        <p>{{ email }} </p>
    </li>
    {{/contacts}}
    </ul>
</div>

I have manually check if the problem is with one of the gems that have been upgrade and found nothing.
The jason object that is generated for the templates is the same for the app with rails 2.3.2 and the app with rails 2.3.6
and I generate it with Rabl.

Problem with slim bars and rails 4

Whenever I use {{outlet}} in a slimbars file I get
Unknown line indicator
(TEMPLATE), Line 4, Column 0
{{outlet}}
^

and if I use ==outlet instead I get
undefined local variable or method `outlet'

Template names include 'javascripts/templates'

Hi!

The readme states the following:

Add the template code to the HandlebarsTemplates global under the name contacts/new

But in my little project this is not the case. Maybe I do something wrong, I would really appreciate if you could take a look.

I have a simple config.ru:

require 'sprockets'
require 'handlebars_assets'

project_root = File.expand_path(File.dirname(__FILE__))
assets = Sprockets::Environment.new(project_root) do |env|
  env.logger = Logger.new(STDOUT)
end

HandlebarsAssets::Config.template_namespace = 'Ember.TEMPLATES'

assets.append_path(File.join(project_root, 'app', 'assets'))
assets.append_path(File.join(project_root, 'app', 'assets', 'javascripts'))
assets.append_path(File.join(project_root, 'app', 'assets', 'stylesheets'))

assets.append_path(File.join(project_root, 'vendor', 'assets'))
assets.append_path(File.join(project_root, 'vendor', 'assets', 'javascripts'))
assets.append_path(File.join(project_root, 'vendor', 'assets', 'stylesheets'))

assets.append_path HandlebarsAssets.path

map "/assets" do
  run assets
end

map "/" do
  run lambda { |env|
    [
      200,
      {
        'Content-Type'  => 'text/html',
        'Cache-Control' => 'public, max-age=86400'
      },
      File.open('public/index.html', File::RDONLY)
    ]
  }
end

When I require all my templates in my application.js.coffee with #= require_tree ./templates I get the following output:

this.Ember.TEMPLATES["javascripts/templates/application"] = ...

I would love to remove the "javascripts/templates" part from the name, is this possible?

Thanks for your time and awesome gem!

Rails 3.1.3 Required Gems are not Being Installed

Maybe I'm doing something wrong, but this is my Gemfile configuration:

group :assets do
  gem 'sass-rails','~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'handlebars_assets'
  gem 'execjs'
end

The problem is that neither bundle install nor bundle update install the Gems at all and not errors are thrown. Any ideas?

wrong compilerInfo generated for external templates.

hey there

just some days ago I upgraded to handlebars_assets 0.12.1 and now I've got a problem with wrong compilerInfo being generated.

I've got a private gem (really just a rails engine file and some templates) and all templates from within the gem are generated with the old compilerInfo, [2, 1.0.0.rc3], causing handlebars to raise an exception.

Here's a accurate summary:

gem 'rails', '3.2.12' 
# ...
# Gemfile, outside of asset group
gem 'handlebars_assets', '0.12.1'

I'm using an initializer to change HandlebarsTemplates to JST:

# config/handlebars_assets.rb
HandlebarsAssets::Config.template_namespace = 'JST'

inside my application.coffee I'm requiring the main file from inside my own gem:

# application.coffee
#= require fail_bowl

the main file in my gem actually requires all necessary templates:

# fail_bowl.coffee
#= require_tree ./templates

Now to the interesting parts:
The app & the gem contain templates -

all templates inside the main application get the following, correct compilerInfo:

this.compilerInfo = [3,'>= 1.0.0-rc.4'];

all templates from the gem get the wrong compiler info:

this.compilerInfo = [2,'>= 1.0.0-rc.3'];

when I change the template the file is regenerated, but the wrong compilerInfo sticks around!

I'm thankful for all suggestions :)

cheers,
Raphael

partials not loading

Hi all -

I updated my gem file, and now my partial handlebar assets aren't loading. I've got a partial in a "/shared" directory that loads fine in development, but not in production (not on heroku nor on a custom linode instance).

Both production sites give this error:

Uncaught Error: The partial nav_strip could not be found

I'm using rails 3.2.6, handlebars_assets 0.5.0, and sprockets 2.1.3
It worked fine using 3.2.6, handlebars_assets 0.5.0, and sprockets 2.1.2 (not sure what combination worked in the past on production, but it did)

Any idea why?

Here's detail on the error:

Uncaught Error: The partial nav_strip could not be found /assets/application-01f8505027960848c2f1be6ecf70aff4.js:20
Handlebars.VM.invokePartial /assets/application-01f8505027960848c2f1be6ecf70aff4.js:20
(anonymous function) /assets/application-01f8505027960848c2f1be6ecf70aff4.js:23
(anonymous function) /assets/application-01f8505027960848c2f1be6ecf70aff4.js:20
Financials.Views.Home.r.render /assets/application-01f8505027960848c2f1be6ecf70aff4.js:23
e /assets/application-01f8505027960848c2f1be6ecf70aff4.js:23
Financials.Routers.Accounts.n.index /assets/application-01f8505027960848c2f1be6ecf70aff4.js:23
s.extend.route /assets/application-01f8505027960848c2f1be6ecf70aff4.js:21
(anonymous function) /assets/application-01f8505027960848c2f1be6ecf70aff4.js:21
S.some.S.any /assets/application-01f8505027960848c2f1be6ecf70aff4.js:21
s.extend.loadUrl /assets/application-01f8505027960848c2f1be6ecf70aff4.js:21
s.extend.start /assets/application-01f8505027960848c2f1be6ecf70aff4.js:21
window.Financials.init /assets/application-01f8505027960848c2f1be6ecf70aff4.js:22
(anonymous function) /assets/application-01f8505027960848c2f1be6ecf70aff4.js:22
s.Callbacks.d /assets/application-01f8505027960848c2f1be6ecf70aff4.js:16
s.Callbacks.v.fireWith /assets/application-01f8505027960848c2f1be6ecf70aff4.js:16
i.extend.ready /assets/application-01f8505027960848c2f1be6ecf70aff4.js:16
n.addEventListener.k

template_name is the full path to template?

The docs says the generate template_name will be:
HandlebarsTemplates'contacts/new';

But I am getting:
templates/common/default_slides
templates/common/error_overlay

Which is full path. I am including using:
//= require_tree ./templates/common

Template assets not recompiling

currently everything works swell in development but it's broken in production. it simply just doesn't want to precompile and include the template asset files while in production.

as such, I'm getting JavaScript errors like this: ReferenceError: HandlebarsTemplates is not defined

I'm having trouble figuring out exactly what needs to be done to allow this to work in production. if you could assist me, it'd be greatly appreciated.

currently, I have handlebar_assets JavaScript assets loading in application.js and I have the template assets in assets/templates

any ideas? where should I start?

thanks in advance

Use with AMD / Require.js / requirejs-rails

I am trying to use handlebars_assets with requirejs-rails + backbone + backbone.marionette and am having a lot of trouble.

It seems like everything is working except I have no access to my template namespace within my require closures.

This stackoverflow question does a pretty good job of going over the problem. However if I use the solution there I lose all of the benefits of handlebars_assets and the asset pipeline.

Is there a way to make handlebars_assets require.js/AMD friendly? Or is it already and I've missed something?

Rails 3.2?

Does this gem work in Rails 3.2? I'm not getting any errors, but I'm not getting it to work either.

Putting gem in assets

group :assets do
gem 'handlebars_assets'
end

If you do that, the rails app will not work in development mode, as handlebars.runtime is not found.

The fix is to not put the gem in the assets group.

This is with rails 3.2.9.

Support for ERB in JavaScript files

If I understand things correctly, Sprockets will process Ruby in JavaScript files if .erb is appended to the end. It seems to me that this can be enabled by simply changing some of the file name checks to allow for .erb to be present at the end, and then Sprockets will handle the rest. Does this seem accurate? Or does handlebars_assets run before that sort of processing would occur? I'd be happy to attempt to implement it if you think it would be useful.

form_helper support

Is there a way to get Rails form helpers (or SimpleForm/ Formtastic for that matter) into the .hbs templates?
This would greatly simplify building forms, since it would take care of routing/ input id-ing etc.

Maybe this should be an opt-in feature for the erb-processor in sprockets - I'm not sure.

JST not available in Javascript

I followed structure in the readme but the JST is not available in javascript (Rails 3.1 with Ruby 1.9.2).
I tried "rake assets:precompile" and it adds Handlebars in the generated application-xxx.js

Luigi

'uninitialized constant Rails' when using without Rails

Hi,

commit @88a2ea5 breaks things for me when using handlebars_assets without Rails. Line 13 seems to be the problem.

irb(main):001:0> defined?(Rails)
=> nil
irb(main):002:0> defined?(::Rails::Engine)
=> nil
irb(main):003:0> defined?(Rails && ::Rails::Engine)
=> "expression"
irb(main):004:0> defined?(Rails) && ::Rails::Engine
=> nil
irb(main):005:0> defined?(Rails) && defined?(::Rails::Engine)
=> nil

Notice the third case, which evaluates to true and subsequently leads to an uninitialized constant Rails error when it does continue with require 'handlebars_assets/engine', which it shouldn't.
Both cases four and five work for me but I haven't tested with Rails.

support for emblem.js?

After playing with hamlbars for a while, I started getting the sense that it was a bit too hacky for my taste.

I recently discovered Emlem.js (https://github.com/machty/emblem.js), which feels a bit like HAML. I was thinking about seeing how hard it would be to hook it into handebars_assets, but before I go too far down this rabbit hole I am curious if you have any thoughts on supporting emblem.js

Info about precompile assets on Heroku with custom config

Hi Les, it turns out the quirks of the assets precompile stage on Heroku can make things a little tricky if you are using HandlebarsAssets to customize the compiler. I thought I'd share the findings, and maybe you could include a note in the docs to make life easier for others.

This is not a bug of any sort, just sharing info gained.

The issue is while in "development" putting the following in an initialiser or config

HandlebarsAssets::Config.options = { data: true }

works perfectly fine, but they won't compile that way on Heroku if you aren't precompiling prior to pushing, because Heroku's compile step runs prior, and usually most people have

config.assets.initialize_on_precompile = false

and HandlebarsAssets is usually in the "assets" group in Gemfile, so not available in Production

My solution is to add a form of the following:

if "assets" == ENV["RAILS_GROUPS"] || ["development", "test"].include?(ENV["RAILS_ENV"])
  HandlebarsAssets::Config.options = { data: true }
end

Now assets:precompile under every scenario I've encountered compiles with the custom options. Hope this is useful.

partials not found exception in Handlebars.js

I've tried several different things, but can't get my partial to be recognized by Handlebars. I'm using handlebars_assets 0.6.6 and rails 3.2.3 (although I've also tried against 3.2.8). My templates are working, just not partials. My partial is in a subdirectory named goals and is named _steps.jst.hbs. I'm trying to access it as goal_steps.

I'm getting an exception in invokePartial() of Handlebars.js that the partial can't be found. The partials array is empty.

I am using the JST namespace. I set a breakpoint in tilt_handlebars after reading another issue, and I see the call to register_partial with the correct partial name.

What else should I try?

Handlebars conditionals don't work as expected

So after some fiddling, I figured out that handlebars conditionals will work if you do this:

| {{#if something}}
h1 This works
| {{/if}}

But why not this?:

{{#if something}}
h1 This does not work
{{/if}}

The above gives me this error:

Unknown line indicator
  (__TEMPLATE__), Line 1, Column 0
    {{#if isNew}}
    ^

I don't know if this is the intended behavior or not... but it seems odd.

Broken: uninitialized constant HandlebarsAssets::Config

I am sure you just forgot to run your test suite ;)

Here is a complementary backtrace. (0.5.0) still works

/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/handlebars_assets-0.6.0/lib/handlebars_assets/tilt_handlebars.rb:50:in `relative_path'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/handlebars_assets-0.6.0/lib/handlebars_assets/tilt_handlebars.rb:54:in `template_name'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/handlebars_assets-0.6.0/lib/handlebars_assets/tilt_handlebars.rb:24:in `evaluate'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/tilt-1.3.3/lib/tilt/template.rb:76:in `render'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/context.rb:177:in `block in evaluate'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/context.rb:174:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/context.rb:174:in `evaluate'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:12:in `initialize'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in `new'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in `block in build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:262:in `circular_call_protection'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:240:in `build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in `block in build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in `build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in `find_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in `find_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:106:in `block in build_required_assets'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:100:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:100:in `build_required_assets'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:16:in `initialize'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in `new'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in `block in build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:262:in `circular_call_protection'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:240:in `build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in `block in build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in `build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in `find_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in `find_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/bundled_asset.rb:16:in `initialize'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:244:in `new'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:244:in `build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in `block in build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in `build_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in `find_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in `find_asset'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/actionpack-3.2.6/lib/sprockets/static_compiler.rb:23:in `block in compile'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:212:in `block in each_logical_path'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:200:in `block (2 levels) in each_file'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:190:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:190:in `each_entry'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:198:in `block in each_file'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:197:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:197:in `each_file'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/sprockets-2.1.3/lib/sprockets/base.rb:210:in `each_logical_path'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/actionpack-3.2.6/lib/sprockets/static_compiler.rb:18:in `compile'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/actionpack-3.2.6/lib/sprockets/assets.rake:56:in `internal_precompile'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/actionpack-3.2.6/lib/sprockets/assets.rake:70:in `block (3 levels) in '
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/trcarden/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@quizzes/gems/actionpack-3.2.6/lib/sprockets/assets.rake:60:in `block (3 levels) in '
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/trcarden/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2.2/bin/rake:33:in `'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/bin/rake:19:in `load'
/Users/trcarden/.rvm/gems/ruby-1.9.2-p180@global/bin/rake:19:in `'
Tasks: TOP => assets:precompile:primary

Creates strange JS output in combination with JST

Hey, I get some strange errors when using this in combination with JST. After a lot of debugging I found, that the pipeline creates code that looks e.g. like this:

Handlebars.template = Handlebars.VM.template;
;
(function() {
  this.JST || (this.JST = {});
  this.JST["users/_chat_message"] = <div class="message {{#user_is_yourself user}}yourself{{/user_is_yourself}}" style="background-image:url('/assets/{{user_avatar_image user "chat"}}');" title="{{created_at}}">
    <div class="popover {{#user_is_yourself user}}right{{else}}left{{/user_is_yourself}}" style="display: block;">
...

So in the 4th line there is an assignment which is just missing some kind of quotes or String delimiter at all. Any ideas on why that is?

Unwanted newline

When using the require directive to embed a template file in another file the initial template source is altered (a newline is appended).

It may seem harmless but it may cause undesired and visible effects.

Example.

# base.js.coffee
#= require templates/foo

text = document.createTextNode(JST['foo']())
el = document.getElementById('container')
el.appendChild(text)
# templates/foo
no newline after this phrase

In the example above the text node will be

"no newline after this phrase
"

instead of "no newline after this phrase"

We have currently decided to trim the compiled template output before using it with text nodes. But is there another way around?

I'm probably doing something incredibly stupid...

But I get this error whenever my app tries to parse a .jst.hbs file:

ActionView::Template::Error (Error: Usage: /var/folders/j7/y4hnjr3j1fq6bhz2zrmrq6sc0000gn/T/execjs20110911-14138-1e6b2y7.js FILE
  (in /Users/chicks/Projects/adjutant/app/assets/javascripts/templates/identities/show.jst.hbs)):
    3: <head>
    4:   <title>Adjutant</title>
    5:   <%= stylesheet_link_tag    "application" %>
    6:   <%= javascript_include_tag "application" %>
    7:   <%= csrf_meta_tags %>
    8: </head>
    9: <body class="container_12">
  app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb__436811332128615094_2173991620'

Manifest file:

    //= require jquery
    //= require jquery_ujs
    //= require handlebars
    //= require ./lib/underscore
    //= require ./lib/backbone
    //= require_tree ./app
    //= require_tree ./models
    //= require_tree ./collections
    //= require_tree ./views
    //= require_tree ./routers
    //= require_tree ./templates

Template file (app/assets/javascripts/templates/show.jst.hbs):

This is {{handlebars}}

I'm on rails 3.1 rc8 and mri 1.9.2

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.