class Sprockets::Context
assets. See ‘DirectiveProcessor` for an example of this.
The `Context` also collects dependencies declared by
<%= asset_url “foo.png” %>
end
def asset_url; end
include MyHelper
environment.context_class.class_eval do
`Context` directly.
by injecting them into `Environment#context_class`. Do not mix them into
They are typically accessed by ERB templates. You can mix in custom helpers
Deprecated: `Context` provides helper methods to all `Template` processors.
def asset_data_uri(path)
$('').attr('src', '<%= asset_data_uri 'avatar.jpg' %>')
#logo { background: url(<%= asset_data_uri 'logo.png' %>) }
Use `asset_data_uri` from ERB with CSS or JavaScript assets:
of the current file.
asset at the specified path, and marks that path as a dependency
Returns a Base64-encoded `data:` URI with the contents of the
def asset_data_uri(path) depend_on_asset(path) asset = environment.find_asset(path, accept_encoding: 'base64') "data:#{asset.content_type};base64,#{Rack::Utils.escape(asset.to_s)}" end
def asset_path(path, options = {})
basics implemention may be provided with different methods that
customized by the application. Though, in the future, some
NOTE: This helper is currently not implemented and should be
Expands logical path to full url to asset.
def asset_path(path, options = {}) message = <<-EOS om asset_path helper is not implemented nd your environment context with a custom method. environment.context_class.class_eval do def asset_path(path, options = {}) end end EOS raise NotImplementedError, message end
def audio_path(path)
def audio_path(path) asset_path(path, type: :audio) end
def depend_on(path)
the dependency file with invalidate the cache of the
This is used for caching purposes. Any changes made to
including it.
`depend_on` allows you to state a dependency on a file without
def depend_on(path) @dependency_paths << resolve(path).to_s nil end
def depend_on_asset(path)
file. Unlike `depend_on`, this will include recursively include
invalidate the dependency asset will invalidate the source
This is used for caching purposes. Any changes that would
without including it.
`depend_on_asset` allows you to state an asset dependency
def depend_on_asset(path) if asset = @environment.find_asset(resolve(path)) @dependency_paths.merge(asset.metadata[:dependency_paths]) end nil end
def font_path(path)
def font_path(path) asset_path(path, type: :font) end
def image_path(path)
def image_path(path) asset_path(path, type: :image) end
def initialize(input)
def initialize(input) @environment = input[:environment] @metadata = input[:metadata] @load_path = input[:load_path] @logical_path = input[:name] @filename = input[:filename] @dirname = File.dirname(@filename) @pathname = Pathname.new(@filename) @content_type = input[:content_type] @required = Set.new(@metadata[:required]) @stubbed = Set.new(@metadata[:stubbed]) @links = Set.new(@metadata[:links]) @dependency_paths = Set.new(@metadata[:dependency_paths]) end
def javascript_path(path)
def javascript_path(path) asset_path(path, type: :javascript) end
def link_asset(path)
easy to construct a link to it.
including it. The target asset is returned from this function making it
`link_asset` declares an external dependency on an asset without directly
def link_asset(path) if asset = @environment.find_asset(resolve(path)) @dependency_paths.merge(asset.metadata[:dependency_paths]) @links << asset.uri end asset end
def metadata
def metadata { required: @required, stubbed: @stubbed, links: @links, dependency_paths: @dependency_paths } end
def require_asset(path)
<%= require_asset "#{framework}.js" %>
require assets.
If ERB processing is enabled, you can use it to dynamically
included once.
dependency will be inserted before the file and will only be
`require_asset` declares `path` as a dependency of the file. The
def require_asset(path) filename = resolve(path, accept: @content_type) @required << @environment.resolve_asset_uri(filename, accept: @content_type, bundle: false) nil end
def resolve(path, options = {})
# => "/path/to/app/javascripts/bar.js"
resolve("./bar.js")
# => "/path/to/app/javascripts/foo.js"
resolve("foo.js")
search.
`:content_type` restriction can be supplied to restrict the
expanded path. Relative paths will also be resolved. An optional
Given a logical path, `resolve` will find and return the fully
def resolve(path, options = {}) options[:content_type] = self.content_type if options[:content_type] == :self options[:accept] = options.delete(:content_type) if environment.absolute_path?(path) path elsif environment.relative_path?(path) path = File.expand_path(path, @dirname) if logical_path = @environment.split_subpath(load_path, path) environment.resolve_in_load_path(load_path, logical_path, options) else raise FileOutsidePaths, "#{path} isn't under path: #{load_path}" end else environment.resolve(path, options) end end
def stub_asset(path)
`path` must be an asset which may or may not already be included
`stub_asset` blacklists `path` from being included in the bundle.
def stub_asset(path) filename = resolve(path, accept: @content_type) @stubbed << @environment.resolve_asset_uri(filename, accept: @content_type, bundle: false) nil end
def stylesheet_path(path)
def stylesheet_path(path) asset_path(path, type: :stylesheet) end
def video_path(path)
def video_path(path) asset_path(path, type: :video) end