0.0
No commit activity in last 3 years
No release in over 3 years
A Rails 3 gem that allows easy inlining of css and JavaScript
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

< 5.0, >= 3.0
 Project Readme

Summary

ScriptHelpers is a gem for Rails 3 that provides some convenient helpers for including JavaScript and CSS in your views.

Install

Just add the following to your Gemfile and bundle:

gem "script_helpers"

Then run:

rails generate script_helpers:install

That will add a few helper calls to your application.html.erb layout. Specifically:

  • script_tag_contents: This helper will return the contents of all the included script tags (ie, calls to script :src => "custom.js").

  • script_contents: This helper will return an embedded script tag with all the script blocks in your view. It will return nil if there are no script blocks to embed.

  • css_tag_contents: Like script_tag_contents, this will return the contents of all the included css tags (ie, calls to css :src => "custom.css").

  • css_contents: Like script_contents, this will return an embedded style tag with all the css blocks in your view. It will return nil if there are no css blocks to embed.

Feel free to move these around after installing.

Usage

After installing, you can use 2 helpers in your views... script and css.

If you want to add content to the overall page script, which will be embedded at the bottom of the page if you do not move the helper calls after installing, you can do the following:

<% script do %>
  alert("I am a script!");
<% end %>

Sometimes it is important to execute a specific script block right after a certain batch of HTML. In that case, use the inline option:

<% script :inline => true do %>
  alert("I am an inline script!");
<% end %>

If you want to add a script file to the block of scripts, you can use the :src option:

<% script :src => "myScript.js" %>

Similarly, the css helper allows block content:

<% css do %>
  .foo { color: red; }
<% end %>

Or including a file:

<% css :src => "myStyling.css" %>

Though, inline styles are not supported right now, as they should probably go with all the rest of your styling.

Caching

If you try to include the same script or css stylesheet twice, the script and css helpers will prevent the second load of the script. This allows you to put your script loading into partials and yet only have the script loaded once.

Thus:

<% script :src => "myScript.js" %>
<% script :src => "myScript.js" %>

will only output 1 instance of myScript.js.