【发布时间】:2017-08-16 22:30:40
【问题描述】:
我正在尝试在我的 scss 中获取 yaml (.yml) 文件的内容。目的是使 yaml 文件的内容可用作 SCSS 映射,以便可以读取和迭代这些值。
到目前为止,我有这个:
SCSS 文件:
$ranges: yaml_load('path/to/file');
Ruby 文件(this gem 的破解版):
require 'yaml'
module Sass::Script::Functions
def yaml_load(file_name)
# Required because the filename is escaped: "\"path/to/file.yml\""
file_name = file_name.to_s[1..-2]
if (File.file?(file_name))
file_content = YAML::load_file(file_name)
if (file_content.is_a?(Hash))
p 'is indeed a hash'
return Sass::Script::Value::Map.new(file_content)
end
p 'Parse error'
else
p 'The requested file could not be found'
end
end
end
我的理解是YAML::load_file 返回一个哈希值,而Sass::Script::Value::Map 将接受一个哈希值作为输入。但是我收到以下我不明白的错误:
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/sass-3.5.1/lib/sass/script/value/map.rb:22:in `block in options=': 未定义的方法 `options=' for "
first.value.in.yaml.file":String (NoMethodError)
使用这样的 yaml 文件:
first.value.in.yaml.file:
name: MyName
color: red
什么是选项方法,我应该如何将 yaml 文件的内容转换为 SASS 映射?
(如果你知道如何正确使用文件名而不被转义,请分享:))
【问题讨论】:
-
如何使用
$ranges变量? -
@aristotll,目前我没有使用它。只是分配已经给出了错误。虽然我假设它可以替换
$map: ( foo: bar );定义,然后我可以使用 map-get 来获取值。