Class: Snp::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/snp/template.rb

Overview

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>...'

Instance Method Summary collapse

Constructor Details

#initialize(template_file, path = Path.new) ⇒ Template

Public: creates a new template instance.

template_file - the basename of the template file.



29
30
31
32
# File 'lib/snp/template.rb', line 29

def initialize(template_file, path = Path.new)
  @file = template_file
  @path = path
end

Instance Method Details

#compile(context) ⇒ Object

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.



39
40
41
42
43
44
45
# 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