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.context_class.class_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.
# File lib/sprockets/context.rb, line 29 29: def initialize(environment, logical_path, pathname) 30: @environment = environment 31: @logical_path = logical_path 32: @pathname = pathname 33: @__LINE__ = nil 34: 35: @_required_paths = [] 36: @_stubbed_assets = Set.new 37: @_dependency_paths = Set.new 38: @_dependency_assets = Set.new([pathname.to_s]) 39: end
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' %>')
# File lib/sprockets/context.rb, line 213 213: def asset_data_uri(path) 214: depend_on_asset(path) 215: asset = environment.find_asset(path) 216: base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "") 217: "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}" 218: end
Tests if target path is able to be safely required into the current concatenation.
# File lib/sprockets/context.rb, line 158 158: def asset_requirable?(path) 159: pathname = resolve(path) 160: content_type = environment.content_type_of(pathname) 161: stat = environment.stat(path) 162: return false unless stat && stat.file? 163: self.content_type.nil? || self.content_type == content_type 164: end
Returns content type of file
'application/javascript' 'text/css'
# File lib/sprockets/context.rb, line 64 64: def content_type 65: environment.content_type_of(pathname) 66: end
`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.
# File lib/sprockets/context.rb, line 114 114: def depend_on(path) 115: @_dependency_paths << resolve(path).to_s 116: nil 117: end
`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.
# File lib/sprockets/context.rb, line 126 126: def depend_on_asset(path) 127: filename = resolve(path).to_s 128: @_dependency_assets << filename 129: nil 130: end
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" %>
# File lib/sprockets/context.rb, line 173 173: def evaluate(path, options = {}) 174: pathname = resolve(path) 175: attributes = environment.attributes_for(pathname) 176: processors = options[:processors] || attributes.processors 177: 178: if options[:data] 179: result = options[:data] 180: else 181: if environment.respond_to?(:default_external_encoding) 182: mime_type = environment.mime_types(pathname.extname) 183: encoding = environment.encoding_for_mime_type(mime_type) 184: result = Sprockets::Utils.read_unicode(pathname, encoding) 185: else 186: result = Sprockets::Utils.read_unicode(pathname) 187: end 188: end 189: 190: processors.each do |processor| 191: begin 192: template = processor.new(pathname.to_s) { result } 193: result = template.render(self, {}) 194: rescue Exception => e 195: annotate_exception! e 196: raise 197: end 198: end 199: 200: result 201: end
Returns logical path without any file extensions.
'app/javascripts/application.js' # => 'application'
# File lib/sprockets/context.rb, line 55 55: def logical_path 56: @logical_path[/^([^.]+)/, 0] 57: end
`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" %>
# File lib/sprockets/context.rb, line 141 141: def require_asset(path) 142: pathname = resolve(path, :content_type => :self) 143: depend_on_asset(pathname) 144: @_required_paths << pathname.to_s 145: nil 146: end
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"
# File lib/sprockets/context.rb, line 79 79: def resolve(path, options = {}, &block) 80: pathname = Pathname.new(path) 81: attributes = environment.attributes_for(pathname) 82: 83: if pathname.absolute? 84: pathname 85: 86: elsif content_type = options[:content_type] 87: content_type = self.content_type if content_type == :self 88: 89: if attributes.format_extension 90: if content_type != attributes.content_type 91: raise ContentTypeMismatch, "#{path} is " + 92: "'#{attributes.content_type}', not '#{content_type}'" 93: end 94: end 95: 96: resolve(path) do |candidate| 97: if self.content_type == environment.content_type_of(candidate) 98: return candidate 99: end 100: end 101: 102: raise FileNotFound, "couldn't find file '#{path}'" 103: else 104: environment.resolve(path, :base_path => self.pathname.dirname, &block) 105: end 106: end
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`.
# File lib/sprockets/context.rb, line 46 46: def root_path 47: environment.paths.detect { |path| pathname.to_s[path] } 48: end
`stub_asset` blacklists `path` from being included in the bundle. `path` must be an asset which may or may not already be included in the bundle.
# File lib/sprockets/context.rb, line 151 151: def stub_asset(path) 152: @_stubbed_assets << resolve(path, :content_type => :self).to_s 153: nil 154: end
Annotates exception backtrace with the original template that the exception was raised in.
# File lib/sprockets/context.rb, line 223 223: def annotate_exception!(exception) 224: location = pathname.to_s 225: location << ":#{@__LINE__}" if @__LINE__ 226: 227: exception.extend(Sprockets::EngineError) 228: exception.sprockets_annotation = " (in #{location})" 229: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.