Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

paulrex/jade-rails

Repository files navigation

Ruby on Rails Integration with Jade

This gem provides integration for Ruby on Rails projects with the Jade templating language.

Combined with the JST engine built in to Sprockets, you can use this gem to render Jade templates anywhere on the front end of your Rails app.

Installing

Add to your Gemfile:

gem 'jade-rails', '~> 1.11.0.1'

In your application.js, require the Jade runtime before any files that include Jade templates.

//= require jade/runtime

Configuring

Use config.jade in your application or environment files to set compilation options. These will be passed to the Jade compiler for all your templates.

This gem supports only a subset of the full list of Jade compiler options, because only some of them make sense for client-side compilation within Rails.

Available Options

  • pretty: Add whitespace to the compiled HTML to make it slightly easier to read. This defaults to true in development and false otherwise.

  • self: Use a self namespace to hold locals in compiled templates. This defaults to false in all environments.

  • compile_debug: Compile templates with debugging instrumentation. (This is passed as compileDebug to the Jade compiler.) It defaults to true in development and false otherwise.

  • globals: This is an array of globals (as strings) that will be made available in the local scope of compiled templates. It defaults to [].

In addition, the filename of the template being compiled is always passed in to the Jade compiler options. If compile_debug is set to true, the filename will be shown as part of the error output.

See the official Jade documentation for more details about these options.

Example

In config/application.rb:

AmazingProject::Application.configure do
  config.jade.pretty = true
  config.jade.compile_debug = true
  config.jade.globals = ['helpers']
end

In app/assets/javascripts/templates/amazing_template.jst.jade:

h1 Jade: A Template Engine
p.lead.
  Jade is a terse and simple templating language with a strong focus on
  performance and powerful features.

Then you can render this template anywhere in your JS code:

JST['templates/amazing_template']()

Notes

Includes are not supported. Instead, use JST along with Jade's functionality for unescaped buffered code. For example, to "include" another template named includes/header.jst.jade which renders with no locals, write:

!= JST['includes/header']()

Running Tests

bundle exec rake test

Versioning

The jade-rails gem version always reflects the version of Jade it contains, with an additional number for gem-specific changes.

Always check the Jade change log when upgrading.

Code Status

Gem Version