class Snp::Template
The Template
class represents a snippet definition through an ERB template. Template
files are looked in a series of directories that can be defined via the SNP_PATH environment variable. By default, these snippet definitions are searched in the `.snp` directory in your home directory.
Examples
t = Snp::Template.new('jquery.erb') t.compile(binding) # => '<html><head>...'
Public Class Methods
new(template_file, path = Path.new)
click to toggle source
Public: creates a new template instance.
template_file - the basename of the template file.
# File lib/snp/template.rb, line 29 def initialize(template_file, path = Path.new) @file = template_file @path = path end
Public Instance Methods
compile(context)
click to toggle source
Public: compiles the template content to an effective snippet, ready to use.
context - a `Binding` object to be used as context in the template compilation.
Returns a string with the compiled template.
# File lib/snp/template.rb, line 39 def compile(context) if template_content ERB.new(template_content, 0, '-').result(context) else raise TemplateNotFound.new(@file, @path.absolute_paths) end end
Private Instance Methods
absolute_path()
click to toggle source
Internal: returns the absolute path to the template, or `nil`, in case it is not found.
# File lib/snp/template.rb, line 58 def absolute_path @path.which(@file, 'erb') end
template_content()
click to toggle source
Internal: returns a string with the content of the template file.
# File lib/snp/template.rb, line 50 def template_content if absolute_path File.read(absolute_path) end end