Embedding Ruby within SASS

115 views
Skip to first unread message

jordanisip

unread,
Jan 17, 2008, 7:42:48 PM1/17/08
to Haml
Hi All,

Is there a way in SASS to do something like this:

!width = params[:width]

OR

#container
:width = params[:width]

Thanks in advance for the help!

Nathan Weizenbaum

unread,
Jan 17, 2008, 7:49:34 PM1/17/08
to ha...@googlegroups.com
No. Sass isn't a view. In production mode, Sass templates are compiled
once, independently of the request cycle, so it doesn't make sense for
them to have access to Ruby variables.

jordanisip

unread,
Jan 17, 2008, 7:52:08 PM1/17/08
to Haml
Ah great, thanks for the clarification.

Rue

unread,
Jan 21, 2008, 10:41:10 AM1/21/08
to Haml
It would be very useful to add application configuration settings at
sass=>css compile time. Otherwise you are forced to replicate your
global settings in your sass instead of simply:

#thing
width: APP_CONFIG['max_width']

I agree that variables don't make a lot of sense (variable css should
be done inline) but setting certain values at compile-time is very
useful to the DRY app.

anyone managed it yet?

I. E. Smith-Heisters

unread,
Jan 21, 2008, 3:48:22 PM1/21/08
to Haml
Just ran across this trying to figure it out. Since it sounds like
there's no solution, I brewed one up:

in lib/sass_ext.rb:
module Sass
module Constant
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/
sass_constants.yml")[RAILS_ENV]
class << self
def get_constant(value, constants)
to_return = constants[value] || APP_CONFIG[value]
raise SyntaxError.new("Undefined constant: \"!#{value}\"")
unless to_return
to_return
end
end
end
end

in config/environment.rb:
require 'sass_ext'

in config/sass_constants.yml:
development:
host: "localhost:3000"
production:
host: "example.com"
test:
host: "localhost:3000"

in a .sass file:
:background-image = "url(http://" + !host + "/images/foobar.png)"

I needed something like that so our CSS could be linked remotely and
still work.

-Ian
Reply all
Reply to author
Forward
0 new messages