Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Last active August 29, 2015 14:01
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseppstein/4ab21dbd5a079ae12f09 to your computer and use it in GitHub Desktop.
Save chriseppstein/4ab21dbd5a079ae12f09 to your computer and use it in GitHub Desktop.
module Sass::Script::Functions
def file_exists(path)
return bool(false) unless options[:filename] # not all sass has a file name (e.g. std input)
current_directory = File.dirname(options[:filename]) rescue nil # a sass filename from an importer may not be processable by File.dirname
return bool(false) unless current_directory && File.exist?(current_directory) # not all sass files are served from the filesystem
full_path = File.expand_path(path.value, current_directory) # a relative path will be expanded against the current_directory of the sass file, symlinks will be followed, absolute paths will be left alone.
return bool(File.exist?(full_path))
end
end

Command line invocation with sass

$ sass -r ./file_exists.rb scss/foo.scss css/foo.css

With Compass in the config.rb

require "./file_exists.rb"

In the Sass file

.foo {
  background: if(file-exists("../images/foo.png"), url("/images/foo.png"), url("/images/generic.png"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment