Class: Hyde::Template Abstract

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

Overview

This class is abstract.

does not represent any actual template engine.

Interface for Template engines

Direct Known Subclasses

Hyde::Templates::ERB, Hyde::Templates::Erubi

Instance Method Summary collapse

Constructor Details

#initialize(input, vars = {}, parent:) ⇒ Template

Returns a new instance of Template.

Parameters:

  • input (String, File)

    template text

  • vars (Hash) (defaults to: {})

    local variables for tempalte

  • parent (Hyde::Node)

    parent node



39
40
41
42
43
44
45
46
47
48
# File 'lib/hyde/template.rb', line 39

def initialize(input, vars = {}, parent:)
  @template = input.is_a?(File) ? input.read : input
  @context = TemplateContext.new(parent, self)
  @parent = parent
  input.close if input.is_a? File
  @binding = @context.binding
  vars.each do |k, v|
    @binding.local_variable_set(k, v)
  end
end

Instance Method Details

#import(filepath) ⇒ Object

Import a template from within current template



85
86
87
88
89
# File 'lib/hyde/template.rb', line 85

def import(filepath)
  newtemp = self.class.new(filepath, {}, parent: @parent)
  newtemp.binding = @binding
  newtemp
end

#local_variable_get(key) ⇒ Object

Get local variable

Parameters:

  • key (Symbol)

Returns:

  • (Object)


60
61
62
# File 'lib/hyde/template.rb', line 60

def local_variable_get(key)
  @binding.local_variable_get(key)
end

#local_variable_set(key, value) ⇒ Object

Set local variable

Parameters:

  • key (Symbol)
  • value (Object)


53
54
55
# File 'lib/hyde/template.rb', line 53

def local_variable_set(key, value)
  @binding.local_variable_set(key, value)
end

#local_variablesArray(Symbol)

Get an array of defined local variables

Returns:

  • (Array(Symbol))


66
67
68
# File 'lib/hyde/template.rb', line 66

def local_variables
  @binding.local_variables
end

#override_locals(vars) ⇒ Object

Override binding variables.

Parameters:

  • vars (Hash{Symbol => Object})


72
73
74
75
76
# File 'lib/hyde/template.rb', line 72

def override_locals(vars)
  vars.each do |k, v|
    @binding.local_variable_set(k, v)
  end
end

#runObject

Note:

This method is a stub.

Run the template



80
81
82
# File 'lib/hyde/template.rb', line 80

def run
  # ... (stub)
end