Class | Yamler::Template |
In: |
lib/yamler/template.rb
|
Parent: | Object |
options | [RW] | Options that are available to the YAML file. |
path | [RW] | The path of the YAML file to be rendered |
Takes the path to the YAML file you wish to render. An optional Hash of options can be passed in. These options are available via the options accessor. If there is a Hash in the options called :locals then the keys of that Hash are available as local methods.
Examples:
Yamler::Template.new('/path/to/file.yml', {:locals => {:username => 'markbates'}, :foo => :bar}) # in file.yml: username: <%= username %> # => 'markbates' foo: <%= options[:foo] %> # => :bar
# File lib/yamler/template.rb, line 23 23: def initialize(path, options = {}) 24: self.path = File.expand_path(path) 25: self.options = options 26: end
Returns the path of the current YAML file.
# File lib/yamler/template.rb, line 61 61: def __FILE__ 62: self.path 63: end
Runs the YAML file through ERB using either the current templates binding or the specified one. This method returns a string and NOT a YAML object.
# File lib/yamler/template.rb, line 31 31: def render(b = binding) 32: res = ERB.new(File.read(self.path)).result(b) 33: res 34: end
Requires another YAML file from inside the current YAML file. The contents of the required YAML file will be run through ERB with the binding of the requiring YAML file and it‘s output will be appended to the calling YAML file. The ’.yml’ extension is optional. It will be added on if the extension is blank. If the file does not exist, it will look for it in the current directory. If it does not exist there it will raise an error.
Examples:
<%= require_yaml('foo') %> # => <current_yml_files_directory>/foo.yml <%= require_yaml('foo.yml') %> # => <current_yml_files_directory>/foo.yml <%= require_yaml('/usr/foo.yml') %> # => /usr/foo.yml
# File lib/yamler/template.rb, line 52 52: def require_yaml(path) 53: path = File.extname(path) == '' ? "#{path}.yml" : path 54: unless File.exists?(path) 55: path = File.expand_path(File.join(File.dirname(self.path), path)) 56: end 57: Yamler::Template.new(path).render(binding) 58: end