Class Sprockets::Context
In: lib/sprockets/context.rb
Parent: Object

`Context` provides helper methods to all `Tilt` processors. They are typically accessed by ERB templates. You can mix in custom helpers by injecting them into `Environment#context_class`. Do not mix them into `Context` directly.

    environment.instance_eval do
      include MyHelper
      def asset_url; end
    end

    <%= asset_url "foo.png" %>

The `Context` also collects dependencies declared by assets. See `DirectiveProcessor` for an example of this.

Methods

Attributes

__LINE__  [W] 
_dependency_assets  [R] 
_dependency_paths  [R] 
_required_paths  [R] 
environment  [R] 
pathname  [R] 

Public Class methods

Public Instance methods

Returns a Base64-encoded `data:` URI with the contents of the asset at the specified path, and marks that path as a dependency of the current file.

Use `asset_data_uri` from ERB with CSS or JavaScript assets:

    #logo { background: url(<%= asset_data_uri 'logo.png' %>) }

    $('<img>').attr('src', '<%= asset_data_uri 'avatar.jpg' %>')

Tests if target path is able to be safely required into the current concatenation.

Returns content type of file

    'application/javascript'
    'text/css'

`depend_on` allows you to state a dependency on a file without including it.

This is used for caching purposes. Any changes made to the dependency file with invalidate the cache of the source file.

`depend_on_asset` allows you to state an asset dependency without including it.

This is used for caching purposes. Any changes that would invalidate the dependency asset will invalidate the source file. Unlike `depend_on`, this will include recursively include the target asset‘s dependencies.

Reads `path` and runs processors on the file.

This allows you to capture the result of an asset and include it directly in another.

    <%= evaluate "bar.js" %>

Returns logical path without any file extensions.

    'app/javascripts/application.js'
    # => 'application'

`require_asset` declares `path` as a dependency of the file. The dependency will be inserted before the file and will only be included once.

If ERB processing is enabled, you can use it to dynamically require assets.

    <%= require_asset "#{framework}.js" %>

Given a logical path, `resolve` will find and return the fully expanded path. Relative paths will also be resolved. An optional `:content_type` restriction can be supplied to restrict the search.

    resolve("foo.js")
    # => "/path/to/app/javascripts/foo.js"

    resolve("./bar.js")
    # => "/path/to/app/javascripts/bar.js"

Returns the environment path that contains the file.

If `app/javascripts` and `app/stylesheets` are in your path, and current file is `app/javascripts/foo/bar.js`, `root_path` would return `app/javascripts`.

[Validate]