Get scss asset w/o compileing
whmii opened this issue Β· comments
Hi there π
I am trying to do an import one of my scss files in to the dom in middleman using middleman-sprockets
. I am working on a documentation site and I need/want to pull in one of my scss files in the assets dir and dump it out on to an example page (in a <code>
block). Based on middleman/middleman#1397 I put together the following in my helpers:
def code_block(path)
asset = sprockets["#{ path }.scss"]
content_tag(:code, asset.to_s)
end
Unfortunately I am unable to get it to give me the scss w/o trying to compile it. It keeps giving me the compiled css instead. It may be that by the time sprockets gets a hold of it, it's already compiled, but I just wanted to make sure I wasn't missing an option or something.
thanks!
Yeah, taking a quick look there isn't something built in to sprockets for grabbing the source -- but it's something you can work around. After finding your asset through sprockets (so sprockets can do it's path finding magic you) you can get it's location on disk w/ #pathname
, then read it with IO
. So something like this:
def code_block path
asset = sprockets["#{path}.scss"]
asset_contents = IO.read(asset.pathname)
content_tag(:code, asset_contents)
end
@stevenosloan that worked perfectly. Thank you!!