diff --git a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb index ad017d76..cc695c8f 100644 --- a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb +++ b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb @@ -1,3 +1,5 @@ +require 'erb' + module Locomotive::Steam module Adapters module Filesystem @@ -34,11 +36,39 @@ def _load(path, frontmatter = false, &block) end end + def parse_erb(data) + def inject(file_path) + File.open(File.join(path, file_path)).read + end + ERB.new(data).result(binding) + end + + def parse_erb_struct(vals) + if vals.is_a? Array + vals.each do | val | + parse_erb_struct(val) + end + else + vals.each do | key, val | + if val.is_a? String + vals[key] = parse_erb(val) + else + parse_erb_struct(val) + end + end + end + end + def safe_yaml_load(yaml, template, path, &block) return {} if yaml.blank? + erb_activated = parse_erb(yaml) != yaml + begin HashConverter.to_sym(YAML.load(yaml)).tap do |attributes| + if erb_activated + parse_erb_struct(attributes) + end block.call(attributes, template) if block_given? end rescue Exception => e @@ -49,7 +79,7 @@ def safe_yaml_load(yaml, template, path, &block) def safe_json_load(path) return {} unless File.exists?(path) - json = File.read(path) + json = parse_erb(File.read(path)) begin MultiJson.load(json)