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.
Requirements
This gem is syntax compatible with Ruby 1.9. It does not run on older Rubies.
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
<div>
<h2 class='name'></h2>
<p class='content'></p>
</div>
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'](directive)
$('body').append(html)
In this example, the result html will be:
<h2 class='name'>Joe<h2>
<p class='content'>I love Smurfs.</p>
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, then 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
Development
This project depends on bundler for installing and using development gems. You'll need to bundle install
and bundle exec rake
to run the tests.
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
- Matt Fletcher ([email protected])
- © 2012 Atomic Object
- More Atomic Object open source projects