在 Middleman 构建期间清理 yaml 数据
Clean up yaml data during build in Middleman
我在 yaml 文件中有一些数据。我想从这些数据中计算一些值,然后我将在从模板构建的页面中使用这些值。到目前为止,我一直在 helpers 中进行此类计算,但它涉及很多重复(例如,同一页面多次进行相同的计算)并且确实是对 helpers 的滥用。
我正在寻找一种更好的替代方法。我想做的是在构建时处理数据文件并进行一次计算,以便在构建页面时可以读取结果(根据需要多次)。
在我的 config.rb 文件中,我设想如下:
#call some code here to process data.products here, either overwriting the products.yml or creating a new file such as products_cleaned.yml
#this code already exists...
data.products.each do |p|
proxy product.url, "/product.html", locals: { product: p}, ignore: true
end
最好的方法是什么?我需要写一个扩展,还是有另一种方法可以调用一些 ruby 来修改我的 yaml 文件?任何帮助将不胜感激。
我需要从外部数据生成 yaml,而我所做的是有一个单独的脚本可以为我生成 yaml 数据文件。然后我会使用 make
或 rake
之类的东西来对这个脚本和 middleman build
的两次调用进行排序。然后我生成的 yaml 从 config.rb 文件中可见。
如果您不想添加构建步骤,那么您可以做的另一件事是处理 data.products 文件,然后自己从中创建 ruby 哈希结构。如果您正在生成 yaml,那么您可以使用 ruby yaml 库将其解析为散列 (http://ruby-doc.org/stdlib-2.5.0/libdoc/psych/rdoc/Psych.html)。然后你可以将这些传递给代理调用。像
#call some code here to process data.products here
my_products = Psych.load(process_products_to_yaml_string(data.products))
#this code already exists...
my_products.each do |p|
proxy p.url, "/product.html", locals: { product: p}, ignore: true
end
我在 yaml 文件中有一些数据。我想从这些数据中计算一些值,然后我将在从模板构建的页面中使用这些值。到目前为止,我一直在 helpers 中进行此类计算,但它涉及很多重复(例如,同一页面多次进行相同的计算)并且确实是对 helpers 的滥用。
我正在寻找一种更好的替代方法。我想做的是在构建时处理数据文件并进行一次计算,以便在构建页面时可以读取结果(根据需要多次)。
在我的 config.rb 文件中,我设想如下:
#call some code here to process data.products here, either overwriting the products.yml or creating a new file such as products_cleaned.yml
#this code already exists...
data.products.each do |p|
proxy product.url, "/product.html", locals: { product: p}, ignore: true
end
最好的方法是什么?我需要写一个扩展,还是有另一种方法可以调用一些 ruby 来修改我的 yaml 文件?任何帮助将不胜感激。
我需要从外部数据生成 yaml,而我所做的是有一个单独的脚本可以为我生成 yaml 数据文件。然后我会使用 make
或 rake
之类的东西来对这个脚本和 middleman build
的两次调用进行排序。然后我生成的 yaml 从 config.rb 文件中可见。
如果您不想添加构建步骤,那么您可以做的另一件事是处理 data.products 文件,然后自己从中创建 ruby 哈希结构。如果您正在生成 yaml,那么您可以使用 ruby yaml 库将其解析为散列 (http://ruby-doc.org/stdlib-2.5.0/libdoc/psych/rdoc/Psych.html)。然后你可以将这些传递给代理调用。像
#call some code here to process data.products here
my_products = Psych.load(process_products_to_yaml_string(data.products))
#this code already exists...
my_products.each do |p|
proxy p.url, "/product.html", locals: { product: p}, ignore: true
end