{"id":13463255,"url":"https://github.com/leshill/handlebars_assets","last_synced_at":"2025-05-14T23:04:55.535Z","repository":{"id":53173560,"uuid":"2249700","full_name":"leshill/handlebars_assets","owner":"leshill","description":"Use handlebars.js templates with the Rails asset pipeline.","archived":false,"fork":false,"pushed_at":"2024-12-16T01:12:39.000Z","size":666,"stargazers_count":646,"open_issues_count":27,"forks_count":159,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-14T04:01:04.018Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leshill.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-08-22T16:16:23.000Z","updated_at":"2024-12-15T22:45:03.000Z","dependencies_parsed_at":"2024-01-13T17:50:42.403Z","dependency_job_id":"c14d49bb-d0b9-4652-9cc8-0eeaf7c88767","html_url":"https://github.com/leshill/handlebars_assets","commit_stats":{"total_commits":296,"total_committers":52,"mean_commits":"5.6923076923076925","dds":"0.45608108108108103","last_synced_commit":"3c6d1662ed3f9c258ec17e935ad15cd5990a6826"},"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshill%2Fhandlebars_assets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshill%2Fhandlebars_assets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshill%2Fhandlebars_assets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leshill%2Fhandlebars_assets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leshill","download_url":"https://codeload.github.com/leshill/handlebars_assets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254243358,"owners_count":22038046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T13:00:49.062Z","updated_at":"2025-05-14T23:04:50.515Z","avatar_url":"https://github.com/leshill.png","language":"Ruby","funding_links":[],"categories":["HTML \u0026 Markup"],"sub_categories":["Template Engines"],"readme":"# Use handlebars.js templates with the asset pipeline and/or sprockets\n\nAre 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?\n\nYea, 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!\n\nUsing `sprockets` with Sinatra or another framework? **handlebars_assets** works outside of Rails too (as of v0.2.0)\n\n# BREAKING CHANGES AS OF v0.17\n\n@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.\n\nIf you have any problems, please post an issue! I will attempt to fix ASAP\n\n# BREAKING CHANGE AS OF v0.9.0\n\nMy pull request to allow `/` in partials was pulled into Handlebars. The hack that converted partial names to underscored paths (`shared/_time` -\u003e `_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.\n\n# Version of handlebars.js\n\n`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.\n\n# Installation\n\n## Rails 4.0+\n\nLoad `handlebars_assets` in your `Gemfile`\n\n```ruby\ngem 'handlebars_assets'\n```\n\nThen follow [Javascript Setup](#javascript-setup)\n\nSide Note: _As of Rails 4.0, the `assets` group is not supported in the Gemfile ([source](http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-3-2-to-rails-4-0-gemfile))._\n\n## Rails 3.1+\n\nLoad `handlebars_assets` in your `Gemfile` as part of the `assets` group\n\n```ruby\ngroup :assets do\n  gem 'handlebars_assets'\nend\n```\n\nThen follow [Javascript Setup](#javascript-setup)\n\n## Sprockets (Non-Rails)\n\n`handlebars_assets` can work with earlier versions of Rails or other frameworks like Sinatra.\n\nLoad `handlebars_assets` in your `Gemfile`\n\n```ruby\ngem 'handlebars_assets'\n```\n\nAdd the `HandlebarsAssets.path` to your `Sprockets::Environment` instance. This\nlets Sprockets know where the Handlebars JavaScript files are and is required\nfor the next steps to work.\n\n```ruby\nenv = Sprockets::Environment.new\n\nrequire 'handlebars_assets'\nenv.append_path HandlebarsAssets.path\n```\n\n## Javascript Setup\n\nRequire `handlebars.runtime.js` in your JavaScript manifest (i.e. `application.js`)\n\n```javascript\n//= require handlebars.runtime\n```\n\nIf you need to compile your JavaScript templates in the browser as well, you should instead require `handlebars.js` (which is significantly larger)\n\n```javascript\n//= require handlebars\n```\n\n### Templates directory\n\nGenerally 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\n\n\u003e app/assets/javascripts/application.js\n```javascript\n//= require_tree ./templates\n```\nThis must be done before `//= require_tree .` otherwise all your templates will not have the intended prefix; and after your inclusion of handlebars/handlebars runtime.\n\n## Rails Asset Precompiling\n\n`handlebars_assets` also works when you are precompiling your assets.\n\n### `rake assets:precompile`\n\nIf you are using `rake assets:precompile`, you have to re-run the `rake` command to rebuild any changed templates. See the [Rails guide](http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets) for more details.\n\n### Heroku \u0026 other cloud hosts\n\nIf you are deploying to Heroku, be sure to read the [Rails guide](http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets) and in your `config/application.rb` set:\n\n```ruby\nconfig.assets.initialize_on_precompile = false\n```\n\nThis avoids running your initializers when compiling assets (see the [guide](http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets) for why you would want that).\n\nHowever, that does mean that you cannot set your configuration in an initializer. This [issue](https://github.com/leshill/handlebars_assets/issues/34) has a workaround, or you can set:\n\n```ruby\nconfig.assets.initialize_on_precompile = true\n```\n\nThis will run all your initializers before precompiling assets.\n\n# Usage\n\n## The template files\n\nWrite your Handlebars templates as standalone files in your templates directory. Organize the templates similarly to Rails views.\n\nFor example, if you have new, edit, and show templates for a Contact model\n\n```\ntemplates/\n  contacts/\n    new.hbs\n    edit.hbs\n    show.hbs\n```\n\nYour file extensions tell the asset pipeline how to process the file. Use `.hbs` to compile the template with Handlebars.\n\nIf your file is `templates/contacts/new.hbs`, the asset pipeline will generate JavaScript code\n\n1. Compile the Handlebars template to JavaScript code\n1. Add the template code to the `HandlebarsTemplates` global under the name `contacts/new`\n\nYou can then invoke the resulting template in your application's JavaScript\n\nNOTE: There will be no javascript object `HandlebarsTemplates` unless at least ONE template is included.\n\n```javascript\nHandlebarsTemplates['contacts/new'](context);\n```\n\n## Partials\n\nIf 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:\n\n```\nInvoke a {{\u003e path/to/_partial }}\n```\n\n# Configuration\n\n## The template namespace\n\nBy default, the global JavaScript object that holds the compiled templates is `HandlebarsTemplates`, but it can\nbe easily renamed. Another common template namespace is `JST`.  Just change the `template_namespace` configuration option\nwhen you initialize your application.\n\n```ruby\nHandlebarsAssets::Config.template_namespace = 'JST'\n```\n\n## Ember Support\n\nTo compile your templates for use with [Ember.js](http://emberjs.com)\nsimply turn on the config option:\n\n```ruby\nHandlebarsAssets::Config.ember = true\n```\n\nIf you need to compile templates for Ember and another framework then enable\nmultiple frameworks:\n\n```ruby\nHandlebarsAssets::Config.multiple_frameworks = true\n```\n\nAfter `mutliple_frameworks` has been enabled templates with the `.ember.hbs`\nextension will be made available to Ember.\n\n## `.hamlbars` and `.slimbars` Support\n\nIf 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.\n\nFor example, if you have a file `widget.hamlbars` that looks like this:\n\n```haml\n%h1 {{title}}\n%p {{body}}\n```\n\nThe Haml will be pre-processed so that the Handlebars template is basically this:\n\n```html\n\u003ch1\u003e {{title}} \u003c/h1\u003e\n\u003cp\u003e {{body}} \u003c/p\u003e\n```\n\nThe same applies to `.slimbars` and the Slim gem. Use `HandlebarsAssets::Config.slim_options` to pass custom options to the Slim rendering engine.\n\n\u003cstrong\u003eNote:\u003c/strong\u003e To use the `hb` handlebars helper with Haml, you'll also need to include the Hamlbars gem in your Gemfile:\n\n```ruby\n  gem 'hamlbars', '~\u003e 2.0'\n```\n\nThis will then allow you to do things like Haml blocks:\n\n```haml\n%ul.authors\n= hb 'each authors' do\n  %li\u003c\n    = succeed ',' do\n      = hb 'lastName'\n    = hb 'firstName'\n```\n\nReference [hamlbars](https://github.com/jamesotron/hamlbars) for more information.\n\n## Using another version of `handlebars.js`\n\nOccasionally 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`.\n\n```ruby\nHandlebarsAssets::Config.compiler = 'my_handlebars.js' # Change the name of the compiler file\nHandlebarsAssets::Config.compiler_path = Rails.root.join('app/assets/javascripts') # Change the location of the compiler file\n```\n\n## Patching `handlebars.js`\n\nIf you need specific customizations to the `handlebars.js` compiler, you can use patch the compiler with your own JavaScript patches.\n\nThe patch file(s) are concatenated with the `handlebars.js` file before compiling. Take a look at the test for details.\n\n```ruby\nHandlebarsAssets::Config.patch_files = 'my_patch.js'\nHandlebarsAssets::Config.patch_path = Rails.root.join('app/assets/javascripts') # Defaults to `Config.compiler_path`\n```\n\n# Thanks\n\nThis gem is standing on the shoulders of giants.\n\nThank you Yehuda Katz (@wycats) for [handlebars.js](https://github.com/wycats/handlebars.js) and lots of other code I use every day.\n\nThank you Charles Lowell (@cowboyd) for [therubyracer](https://github.com/cowboyd/therubyracer) and [handlebars.rb](https://github.com/cowboyd/handlebars.rb).\n\n# Maintainer\n\nThis gem is maintained by Alex Riedler [Github](https://github.com/AlexRiedler).\n\n# Author\n\nLes Hill, follow me on [Github](https://github.com/leshill) and [Twitter](https://twitter.com/leshill).\n\n# Contributors\n\n* Matt Burke         (@spraints)       : execjs support\n*                    (@kendagriff)     : 1.8.7 compatibility\n* Thorben Schröder   (@walski)         : 3.1 asset group for precompile\n* Erwan Barrier      (@erwanb)         : Support for plain sprockets\n* Brendan Loudermilk (@bloudermilk)    : HandlebarsAssets.path\n* Dan Evans          (@danevans)       : Rails 2 support\n* Ben Woosley        (@empact)         : Update to handlebars.js 1.0.0.beta.6\n*                    (@cw-moshe)       : Remove 'templates/' from names\n* Spike Brehm        (@spikebrehm)     : Config.template\\_namespace option\n* Ken Mayer          (@kmayer)         : Quick fix for template\\_namespace option\n* Brad Murray        (@wyaeld)         : Generic options support\n* Blake Williams     (@BlakeWilliams)  : .handlebars extension\n* Tristan Koch       (@trkoch)         : Strip leading whitespace from compiled templates\n* Brian Cardarella   (@bcardarella)    : Ember support\n* David Lee          (@davidlee)       : Slim support\n* Phil Cohen         (@phlipper)       : README cleanup\n* Akshay Rawat       (@akshayrawat)    : Update to handlebars.js 1.0.0-rc.3\n* Turadg Aleahmad    (@turadg)         : Update to handlebars 1.0.0-rc.4\n* Mark Rushakoff     (@mark-rushakoff) : Synchronize Sprockets engine registers with Rails\n* Alex Riedler       (@AlexRiedler)    : Pass scope and locals up the chain\n* lee                (@lee)            : Update to handlebars 1.0.0\n* Ken Collins        (@metaskills)     : Register with Sprockets\n* Blake Hitchcock    (@rbhitchcock)    : Support ember and other handlebars use simultaneously\n* ajk                (@Darep)          : Fix .hbs extension for multi-framework support\n*                    (@mattmenefee)    : README section for `hamlbars`\n* Parker Selbert     (@sorentwo)       : README section for `multiple_frameworks`\n* Francisco QV       (@panchoqv)       : README clarification\n* Dylan Markow       (@dmarkow)        : README cleanup\n* Peter Boling       (@pboling)        : AMD Loading\n\n# Contributing\n\nPull requests are welcome! Please do not update the version number.\n\nIn a nutshell:\n\n1. Fork\n1. Create a topic branch - git checkout -b my_branch\n1. Push to your branch - git push origin my_branch\n1. Create a Pull Request from your branch\n1. That's it!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleshill%2Fhandlebars_assets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleshill%2Fhandlebars_assets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleshill%2Fhandlebars_assets/lists"}