jquery.expand assets

A Sprockets processor that wraps html in a jquery.expand function. It is specifically meant to work within the Rails asset pipeline.

The processor outputs CoffeeScript, so you'll need to have the result be processed by the CoffeeScript processor.

Creating and using templates

Create your templates in a directory named app/assets/javascripts/templates. Organize your templats within subdirectories. I personally organize them based on what resource they're associated with.

Write your template and wrap it in a root element.

Example: app/assets/javascripts/templates/articles/_entry.js.coffee.expand

Your template is now available to Javascript as a function on the JST object. Pass an expand directive to the function and you'll be given the result minus the wrapper element.

Example: see articles/_entry.js.coffee.expand above directive = { name: 'Joe', content: 'I love Smurfs.' } html = JST'articles/entry' $('body').append(html)

In this example, the result html will be:

Joe

I love Smurfs.

Naming conventions

Templates are named on the JST object based on their filename. Directories under app/assets/javascripts/templates are preserved. If the template's filename starts with an underscore, the underscore is removed from the name.

Examples:

articles/_entry.js.coffee.expand becomes articles/entry
articles/comments/_comment.js.coffee.expand becomes articles/comments/comment
articles/response.js.coffee.expand becomes articles/response

Resources

Thanks so much!

I'd like to give a huge 'thank you' to the Sprockets, haml_assets, and haml_coffee_assets projects. Without their examples, I'm not sure I would have figured out the code required to create the Rails engine and Tilt template.

Authors