Skip to content

Use Slim with Rails helpers in the asset pipeline

Notifications You must be signed in to change notification settings

mattvague/slim_assets

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Use slim to write your Javascript templates

ALPHA

Build Status

Writing Javascript templates for Backbone.js (or other frameworks) in your app? Would you like to use slim and the asset pipeline?

This gem adds slim templating support to the Rails 3.1 asset pipeline. This gem works with the EJS gem and JST asset engine to make your slim available as a compiled Javascript template.

Installing

Add this to your Gemfile

gem 'slim_assets'
gem 'ejs'
gem 'slim'

Using slim for your Javascript templates

Templates directory

You should locate your templates under app/assets; we suggest app/assets/templates. In your Javascript manifest file (for example application.js), use require_tree

//= require_tree ../templates

The template file

Inside your templates directory, add your template file. The file should be named as follows

your_template_name.jst.ejs.slim

The asset pipeline will then generate the actual Javascript asset

  1. Convert your slim to HTML
  2. Compile the HTML to an EJS Javascript template
  3. Add the template to the JST global under the templates name

Important! The asset pipeline is not invoking a controller to generate the templates. If you are using existing view templates, you may have to edit templates to remove some references to controller helpers.

EJS

In your template file you can use the EJS delimiters as you would normally. If you want to use them in attributes mark the attribute html_safe.

= f.text_field :email, class: 'text', value: '<%= email %>'.html_safe

Helpers

All the ActionView and route helpers are available in your template. If you use form_for and the related helpers, you should use the new object form, even if you are writing an edit form, for example

= form_for :contact, url: "javascript_not_working", html: {:class => :edit_contact, :method => :put} do |f|
  %p
    = f.label :name, "Name"
    = f.text_field :name, class: 'text required', autofocus: true, value: '<%= name %>'.html_safe

Credits

  • Les Hill : @leshill
  • Wes Gibbs : @wgibbs

This gem is heavily based on their haml_assets gem

About the Author

Crowd Interactive is a web design and development company that happens to work in Colima, Mexico. We specialize in building and growing online retail stores. We don’t work with everyone – just companies we believe in. Call us today to see if there’s a fit. Find more info here!

About

Use Slim with Rails helpers in the asset pipeline

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%